#include #include using namespace std; //ifstream fin("ferma.in"); //ofstream fout("ferma.out"); const int Dx[] = {-1, 0, 0, 1}; const int Dy[] = {0, -1, 1, 0}; char A[405][405], snul[11], solc; int N, M, q; int maxim, solx, soly; int nr, marime; int B[405][405]; bool used[160800]; struct zone { char cul; int mar; }; zone V[160800]; void fill(int i, int j) { ++marime; B[i][j] = nr; for (int k = 0; k < 4; ++k) { int ii = i + Dx[k]; int jj = j + Dy[k]; if ((ii > N || ii < 1) || (jj > M || jj < 1)) continue; if (A[ii][jj] == A[i][j] && !B[ii][jj]) fill(ii, jj); } } void check(int i, int j) { for (int k = 0; k < 4; ++k) { int i1 = i + Dx[k]; int j1 = j + Dy[k]; if ((i1 < 1 || i1 > N) || (j1 < 1 || j1 > M)) continue; int sol = V[B[i1][j1]].mar; for (int kk = 0; kk < 4; ++kk) { int i2 = i + Dx[kk]; int j2 = j + Dy[kk]; if ((i2 < 1 || i2 > N) || (j2 < 1 || j2 > M)) continue; if (i1 == i2 && j1 == j2) continue; if (V[B[i1][j1]].cul == V[B[i2][j2]].cul && B[i1][j1] != B[i2][j2] && !used[B[i][j]]) { sol += V[B[i2][j2]].mar; used[B[i2][j2]] = true; } } for (int kk = 0; kk < 4; ++kk) { int i2 = i + Dx[kk]; int j2 = j + Dy[kk]; if ((i2 < 1 || i2 > N) || (j2 < 1 || j2 > M)) continue; used[B[i2][j2]] = false; } if (sol > maxim) { solx = i; soly = j; solc = V[B[i1][j1]].cul; maxim = sol; } } } int main() { //fin >> q; //fin >> N >> M; //fin.getline(snul, 11); cin >> q; cin >> N >> M; cin.getline(snul, 11); for (int i = 1; i <= N; ++i) cin.getline(A[i] + 1, M + 5); //fin.getline(A[i] + 1, M + 5); for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) if (!B[i][j]) { ++nr; marime = 0; fill(i, j); V[nr].cul = A[i][j]; V[nr].mar = marime; } maxim = 0; if (q == 1) { for (int i = 1; i <= nr; ++i) maxim = max(maxim, V[i].mar); //fout << maxim << '\n'; cout << maxim << '\n'; } else { for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) check(i, j); //fout << solx << ' ' << soly << '\n'; //fout << solc << '\n'; cout << solx << ' ' << soly << '\n'; cout << solc << '\n'; } return 0; }