#include #include #include #include #define REP(i,x) for(int i=0 ; i<(int)(x) ; i++) #define LL long long using namespace std; int M,N; vector S; //struct Info{ // int x,y,k,c; //}; // // //vector > I; // //void read(){ // I.clear(); // // bool al[26]; // memset(al,false,sizeof(al)); // int alsize=0; // for(int y=0;y next; // if(S[ny][nx]==S[y][x]){ // if(x==nx){ // for(int i=y+1;i pos[26]; REP(i,26)pos[i].clear(); REP(y,M)REP(x,N){ if(S[y][x]!='.'){ pos[S[y][x]-'A'].push_back(P(y,x)); } } int res = 0; bool used[26]; memset(used,false,sizeof(used)); while(true){ bool update = false; REP(a,26){ if(used[a])continue; bool ok = true; if(pos[a].size()==0)continue; P s = pos[a][0]; P e = pos[a][1]; if(s.x==e.x){ for(int y=min(s.y+1,e.y+1) ; ye.x)x--; if(ys.y)y--; if(x==e.x&&y==s.y)break; } x = e.x,y = s.y; while(true){ if(!(S[y][e.x]=='.'||S[y][e.x]==a+'A'))f2=false; if(!(S[s.y][x]=='.'||S[s.y][x]==a+'A'))f2=false; if(xs.x)x--; if(ye.y)y--; if(x==s.x&&y==e.y)break; } if(!f1&&!f2)ok=false; } if(ok){ used[a] = true; update = true; S[s.y][s.x] = '.'; S[e.y][e.x] = '.'; res+=2; } } if(!update)break; } cout << res << endl; return res; } int main(){ while(cin>>M>>N){ S.clear(); for(int i=0;i>temp; S.push_back(temp); } solve(); } return 0; }