#include "stdio.h"
#include "map"
#include "string"
#include "iostream"
using namespace std;
int u[4] = {0, 0, 1, -1};
int v[4] = {1, -1, 0, 0};
// global
map<string, int> g_SCORE;
int g_BEST_SCORE;
int g_mapSize, g_mapSize_old;
// funcs definition
void globalInit()
{
g_SCORE.clear();
g_BEST_SCORE = 1;
g_mapSize = 0;
g_mapSize_old = 0;
}
string readMapsRslt()
{
string x = "2441353511212251124434445434222131423443424144521211224115341251131145244144441542544531313321521455";
return x;
}
bool checkSingle(string tmpState, int p)
{
int x = p/10 + 1;
int y = p%10 + 1;
for(int i=0; i<4; ++i) {
int nx = x + u[i];
int ny = y + v[i];
if ( nx>=1 && nx<=10 && ny>=1 && ny<=10 ) {
int np = (nx-1)*10+ny-1;
if ( tmpState[p] == tmpState[np] ) return false;
}
}
return true;
}
int destroy(string &State, int p)
{
int Q[105];
int head=0;
int tail=1;
char COLOR = State[p];
Q[tail] = p;
State[p] = '0';
while( head<tail ) {
++head;
int pp = Q[head];
int x = pp/10 + 1;
int y = pp%10 + 1;
for(int i=0; i<4; ++i) {
int nx = x + u[i];
int ny = y + v[i];
if ( nx>=1 && nx<=10 && ny>=1 && ny<=10 ) {
int np = (nx-1)*10+ny-1;
if ( State[np] == COLOR ) {
Q[++tail] = np;
State[np] = '0';
}
}
}
}
return tail;
}
bool colIsEmpty(string &State, int col)
{
for( int x=1; x<=10; ++x) {
int p = (x-1)*10+col-1;
if( State[p] != '0' ) {
return false;
}
}
return true;
}
void update(string &State)
{
for (int x=10; x>=1; --x) {
for (int y=1; y<=10; ++y) {
int p = (x-1)*10+y-1;
if ( State[p] == '0' ) continue;
while ( p+10<100 && State[p+10] == '0' ) {
State[p+10] = State[p];
State[p] = '0';
p += 10;
}
}
}
for (int y=1; y<=10; ++y) {
if ( colIsEmpty(State, y) == true ) continue;
int tmpy = y;
while ( tmpy > 1 && colIsEmpty(tmpy-1) == true ) {
for(int x=1; x<=10; ++x) {
int p = (x-1)*10+tmp-1;
State[p-1] = State[p];
State[p] = '0';
}
tmpy--;
}
}
}
void printt(string state, int score)
{
cout << "SCORE: " << score << " mapSize: " << g_mapSize << endl;
for (int x=1; x<=10; ++x) {
for (int y=1; y<=10; ++y) {
int p = (x-1)*10+y-1;
if ( state[p] == '0' ) cout << " "; else cout << state[p];
}
cout << endl;
}
cout << "---------------------------------" << endl;
}
int exploreFromState(string state)
{
int sco = g_SCORE[state];
if ( sco != 0 ) {
return sco;
}
string tmpState = state;
int tmpMax = 1;
for (int p=0; p<100; ++p) {
if ( tmpState[p] == '0' ) continue;
if ( checkSingle(state, p) == true ) continue;
destroy(tmpState, p);
string state2 = state;
int cnt = destroy(state2, p);
update(state2);
tmpMax = max( tmpMax, exploreFromState(state2) + 5*cnt*cnt);
}
g_SCORE[state] = tmpMax;
g_mapSize++;
if(tmpMax > g_BEST_SCORE) {
g_BEST_SCORE = tmpMax;
//printt(state, g_BEST_SCORE);
//cout << state << " : " << g_BEST_SCORE << endl;
//g_BEST_SCORE = 999999;
}
printt(state, g_BEST_SCORE);
return tmpMax;
}
void proc(string initState)
{
g_BEST_SCORE = exploreFromState(initState);
cout << g_BEST_SCORE << endl;
}
void test1()
{
string x = "2441353511212251124434445434222131423443424144521211224115341251131145244144441542544531313321521400";
update(x);
printt(x, 1);
}
int main()
{
//test1();
globalInit();
string initState = readMapsRslt();
proc(initState);
return 0;
}