#include #include #include using namespace std; ofstream g("pharm.out"); long n,m,i,j,nr,mx2,t,nrp,mat[401][401],mat2[401][401],mat3[401][401],savi,savj; char s[401],sav,mats[401][401]; void fill(long i,long j) { long sav; sav=mat[i][j]; mat3[i][j]=nrp; mat[i][j]=-1; if (mat[i+1][j]==sav) { fill(i+1,j); nr++; } if (mat[i-1][j]==sav) { fill(i-1,j); nr++; } if (mat[i][j+1]==sav) { fill(i,j+1); nr++; } if (mat[i][j-1]==sav) { fill(i,j-1); nr++; } } void fill2(long i,long j) { mat[i][j]=nr+1000; if (mat[i+1][j]==-1) fill2(i+1,j); if (mat[i-1][j]==-1) fill2(i-1,j); if (mat[i][j+1]==-1) fill2(i,j+1); if (mat[i][j-1]==-1) fill2(i,j-1); } long rezolva1() { long mx=0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { nr=0; if (mat[i][j]!=-1 && mat[i][j]<1000) { nrp++; fill(i,j); nr++; fill2(i,j); } if(nr>mx) mx=nr; } return mx; } int main() { freopen("pharm.in","r",stdin); scanf("%ld%ld%ld",&t,&n,&m); for (i=1;i<=n;i++) { scanf("%s",&s); strcpy(mats[i],s); for (j=1;j<=m;j++) mat[i][j]=mat2[i][j]=s[j-1]-'a'+1; } for (i=1;i<=m;i++) mat[0][i]=mat[n+1][i]=-2; for (i=1;i<=n;i++) mat[i][0]=mat[i][m+1]=-2; if(t==1) g<mx2) { mx2=nr; savi=i; savj=j; sav=mats[i-1][j-1]; } } if (mat3[i+1][j]!=mat3[i][j]) { nr=mat[i+1][j]-1000; nr+=mat[i][j]-1000; if (mat3[i-1][j]!=mat3[i][j] && mat2[i-1][j]==mat2[i+1][j]) nr+=mat[i-1][j]-1000; if (mat3[i][j-1]!=mat3[i][j] && mat2[i][j-1]==mat2[i+1][j]) nr+=mat[i][j-1]-1000; if (mat3[i][j+1]!=mat3[i][j] && mat2[i][j+1]==mat2[i+1][j]) nr+=mat[i][j+1]-1000; if (nr>mx2) { mx2=nr; savi=i; savj=j; sav=mats[i+1][j-1]; } } if (mat3[i][j+1]!=mat3[i][j]) { nr=mat[i][j+1]-1000; nr+=mat[i][j]-1000; if (mat3[i+1][j]!=mat3[i][j] && mat2[i+1][j]==mat2[i][j+1]) nr+=mat[i+1][j]-1000; if (mat3[i][j-1]!=mat3[i][j] && mat2[i][j-1]==mat2[i][j+1]) nr+=mat[i][j-1]-1000; if (mat3[i-1][j]!=mat3[i][j] && mat2[i][j+1]==mat2[i][j+1]) nr+=mat[i-1][j]-1000; if (nr>mx2) { mx2=nr; savi=i; savj=j; sav=mats[i][j]; } } if (mat3[i][j-1]!=mat3[i][j]) { nr=mat[i][j-1]-1000; nr+=mat[i][j]-1000; if (mat3[i+1][j]!=mat3[i][j] && mat2[i+1][j]==mat2[i][j-1]) nr+=mat[i+1][j]-1000; if (mat3[i-1][j]!=mat3[i][j] && mat2[i][j-1]==mat2[i][j-1]) nr+=mat[i-1][j]-1000; if (mat3[i][j+1]!=mat3[i][j] && mat2[i][j+1]==mat2[i][j-1]) nr+=mat[i][j+1]-1000; if (nr>mx2) { mx2=nr; savi=i; savj=j; sav=mats[i][j-2]; } } } g<