#include <bits/stdc++.h>
#define int long long
using namespace std;
struct info {
int x, y;
};
int m, n;
int a[1111][1111];
int lab[1111 * 1111];
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
unordered_map<int, int> freq;
int getIndex(int x, int y) {
return (x - 1) * n + y;
}
void init() {
for (int i = 0; i < m * n; i += 1) {
lab[i] = -1;
}
}
int Find(int u) {
if (lab[u] < 0) return u;
lab[u] = Find(lab[u]);
return lab[u];
}
void Union(int u, int v) {
int r1 = Find(u);
int r2 = Find(v);
if (r1 == r2) return;
if (abs(r1) < abs(r2)) swap(r1, r2);
lab[r1] += lab[r2];
lab[r2] = r1;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("ISLANDS.INP", "r", stdin);
freopen("ISLANDS.OUT", "w", stdout);
cin >> m >> n;
init();
int high = 0;
for (int i = 1; i <= m; i += 1) {
for (int j = 1; j <= n; j += 1) {
cin >> a[i][j];
high = max(high, a[i][j]);
freq[a[i][j]] += 1;
}
}
int res = 0;
for (int k = high; k >= 0; k -= 1) {
if (freq[k] > 0) {
unordered_set<int> cnt;
for (int i = 1; i <= m; i += 1) {
for (int j = 1; j <= n; j += 1) {
if (a[i][j] < k) continue;
for (int d = 0; d < 4; d += 1) {
info h = {i + dx[d], j + dy[d]};
if (h.x < 1 || h.y < 1 || h.x > m || h.y > n) continue;
if (a[h.x][h.y] >= k) {
Union(getIndex(i, j), getIndex(h.x, h.y));
}
}
}
}
for (int i = 1; i <= m; i += 1) {
for (int j = 1; j <= n; j += 1) {
if (a[i][j] >= k) {
cnt.insert(Find(getIndex(i, j)));
}
}
}
res = max(res, (int)cnt.size());
}
}
cout << res;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgaW50IGxvbmcgbG9uZwoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBpbmZvIHsKICAgIGludCB4LCB5Owp9OwoKaW50IG0sIG47CmludCBhWzExMTFdWzExMTFdOwppbnQgbGFiWzExMTEgKiAxMTExXTsKaW50IGR4W10gPSB7MCwgMCwgMSwgLTF9OwppbnQgZHlbXSA9IHstMSwgMSwgMCwgMH07Cgp1bm9yZGVyZWRfbWFwPGludCwgaW50PiBmcmVxOwoKaW50IGdldEluZGV4KGludCB4LCBpbnQgeSkgewogICAgcmV0dXJuICh4IC0gMSkgKiBuICsgeTsKfQoKdm9pZCBpbml0KCkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtICogbjsgaSArPSAxKSB7CiAgICAgICAgbGFiW2ldID0gLTE7CiAgICB9Cn0KCmludCBGaW5kKGludCB1KSB7CiAgICBpZiAobGFiW3VdIDwgMCkgcmV0dXJuIHU7CgogICAgbGFiW3VdID0gRmluZChsYWJbdV0pOwoKICAgIHJldHVybiBsYWJbdV07Cn0KCnZvaWQgVW5pb24oaW50IHUsIGludCB2KSB7CiAgICBpbnQgcjEgPSBGaW5kKHUpOwogICAgaW50IHIyID0gRmluZCh2KTsKCiAgICBpZiAocjEgPT0gcjIpIHJldHVybjsKCiAgICBpZiAoYWJzKHIxKSA8IGFicyhyMikpIHN3YXAocjEsIHIyKTsKCiAgICBsYWJbcjFdICs9IGxhYltyMl07CiAgICBsYWJbcjJdID0gcjE7Cn0KCmludDMyX3QgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKCiAgICBmcmVvcGVuKCJJU0xBTkRTLklOUCIsICJyIiwgc3RkaW4pOwogICAgZnJlb3BlbigiSVNMQU5EUy5PVVQiLCAidyIsIHN0ZG91dCk7CgogICAgY2luID4+IG0gPj4gbjsKCiAgICBpbml0KCk7CgogICAgaW50IGhpZ2ggPSAwOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbTsgaSArPSAxKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbjsgaiArPSAxKSB7CiAgICAgICAgICAgIGNpbiA+PiBhW2ldW2pdOwogICAgICAgICAgICBoaWdoID0gbWF4KGhpZ2gsIGFbaV1bal0pOwogICAgICAgICAgICBmcmVxW2FbaV1bal1dICs9IDE7CiAgICAgICAgfQogICAgfQoKICAgIGludCByZXMgPSAwOwoKICAgIGZvciAoaW50IGsgPSBoaWdoOyBrID49IDA7IGsgLT0gMSkgewogICAgICAgIGlmIChmcmVxW2tdID4gMCkgewoKICAgICAgICAgICAgdW5vcmRlcmVkX3NldDxpbnQ+IGNudDsKCiAgICAgICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG07IGkgKz0gMSkgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbjsgaiArPSAxKSB7CgogICAgICAgICAgICAgICAgICAgIGlmIChhW2ldW2pdIDwgaykgY29udGludWU7CgogICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGQgPSAwOyBkIDwgNDsgZCArPSAxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGluZm8gaCA9IHtpICsgZHhbZF0sIGogKyBkeVtkXX07CgogICAgICAgICAgICAgICAgICAgICAgICBpZiAoaC54IDwgMSB8fCBoLnkgPCAxIHx8IGgueCA+IG0gfHwgaC55ID4gbikgY29udGludWU7CgogICAgICAgICAgICAgICAgICAgICAgICBpZiAoYVtoLnhdW2gueV0gPj0gaykgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgVW5pb24oZ2V0SW5kZXgoaSwgaiksIGdldEluZGV4KGgueCwgaC55KSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCgogICAgICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBtOyBpICs9IDEpIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSAxOyBqIDw9IG47IGogKz0gMSkgewogICAgICAgICAgICAgICAgICAgIGlmIChhW2ldW2pdID49IGspIHsKICAgICAgICAgICAgICAgICAgICAgICAgY250Lmluc2VydChGaW5kKGdldEluZGV4KGksIGopKSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICByZXMgPSBtYXgocmVzLCAoaW50KWNudC5zaXplKCkpOwogICAgICAgIH0KICAgIH0KCiAgICBjb3V0IDw8IHJlczsKCiAgICByZXR1cm4gMDsKfQ==