#include #define endl "\n" using namespace std; typedef long long ll; typedef vector vl; typedef pair PP; #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") #pragma comment(linker, "/stack:200000000") #define overload4(_1, _2, _3, _4, name, ...) name #define overload3(_1, _2, _3, name, ...) name #define rep1(n) for (ll i = 0; i < n; ++i) #define rep2(i, n) for (ll i = 0; i < n; ++i) #define rep3(i, a, b) for (ll i = a; i < b; ++i) #define rep4(i, a, b, c) for (ll i = a; i < b; i += c) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(n) for (ll i = n; i--;) #define rrep2(i, n) for (ll i = n; i--;) #define rrep3(i, a, b) for (ll i = b; i-- > (a);) #define rrep4(i, a, b, c) \ for (ll i = (a) + ((b) - (a)-1) / (c) * (c); i >= (a); i -= c) #define rrep(...) \ overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define elif else if ll beki(ll i) { return 1ll << i; } #define skip(M) \ if (M) \ continue #define all(v) v.begin(), v.end() #define allm(x, y, M) for (auto [(x), (y)] : (M)) #define alls(i, S) for (auto(&i) : (S)) #define pb push_back #define fi first #define se second const ll INF = (1LL << 61) - 1; const ll MOD = 1000000007; const ll MOD2 = 998244353; const ll MAX_N1 = 200010; const ll MAX_N2 = 500010; template void print(X x) { cout << x << endl; } void print(vl x) { for (ll i : x) { cout << i << " "; } cout << endl; } void print(vector x) { for (PP i : x) { cout << i.first << " " << i.second << endl; } cout << endl; } template void printr(X x) { cerr << x << endl; } void printr(vl x) { for (ll i : x) { cerr << i << " "; } cerr << endl; } void printr(vector x) { for (PP i : x) { cerr << i.first << " " << i.second << endl; } cout << endl; } template int lbound(vector &Y, X a) { return lower_bound(all(Y), a) - Y.begin(); } template int ubound(vector &Y, X a) { return upper_bound(all(Y), a) - Y.begin(); } template X lbound2(vector &Y, X a, int mode = 0) { int x = lbound(Y, a) - mode; if (x < 0 || x >= Y.size()) return INF; return Y[x]; } template X ubound2(vector &Y, X a, int mode = 0) { int x = ubound(Y, a) - mode; if (x < 0 || x >= Y.size()) return INF; return Y[x]; } template void cl(T &... t) { (..., (t.clear())); } template void in(T &... t) { (..., (cin >> t)); } template void put(vl &V, int n, int mode = 0) { ll k; if (mode == 0) cl(V); rep(i, n) { cin >> k; V.pb(k); } } int max_index(vl &V) { return max_element(all(V)) - V.begin(); } int min_index(vl &V) { return min_element(all(V)) - V.begin(); } ll sum(vl &V) { return accumulate(all(V), 0ll); } template void UNIQUE(vector &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } template T ADD(T a, T b) { T res; return __builtin_add_overflow(a, b, &res) ? numeric_limits::max() : res; } template T MUL(T a, T b) { T res; return __builtin_mul_overflow(a, b, &res) ? numeric_limits::max() : res; }; template T POW(T a, int n) { T res = 1; for (; n > 0; n >>= 1, a *= a) if (n & 1) res *= a; return res; } template bool ma(X &a, X b) { if (a < b) { a = b; return true; } return false; } template bool mi(X &a, X b) { if (a > b) { a = b; return true; } return false; } void vset(vl &A, ll n, ll k) { A = vl(n, k); } void so(vl &A) { sort(all(A)); } ll age(ll x, ll y) { return (x + y - 1) / y; } ll a, b, c, d, aa, bb, cc, dd, h, x, y, z, p, q, n, t, r, k, w, l, ans, m, u, v; ll codeforces = 1; string S, T, U; vl A, B, C, D; vl g[MAX_N2]; vector VP, VP2; set s1, s2; class UnionFind { // まとめる 判定 サイズを知る public: // Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。 vector Parent; // 初期化 UnionFind(ll N) { Parent = vector(N, -1); } // Aのrootを調べる int root(int A) { if (Parent[A] < 0) return A; // マイナスならそれはroot return Parent[A] = root(Parent[A]); } // rootの値をプラスに戻して返す(サイズ) ll size(int A) { return -Parent[root(A)]; } // くっつける関数 bool connect(int A, int B) { // AとBのroot同士をくっつける A = root(A); // ここのAは、"rootの場所"の意味 B = root(B); if (A == B) return false; // 既にくっついている if (size(A) < size(B)) swap(A, B); // 大きい方にくっつけるために中身交換 Parent[A] += Parent[B]; // 中身更新 Parent[B] = A; return true; } }; void solve() { in(h, w, n); UnionFind UNI(h * w); vector> TT; vector TTT[h * w]; ll masu[h][w], saisyou[h * w]; rep(i, h) { rep(j, w) { in(masu[i][j]); TTT[masu[i][j] - 1].pb({i, j}); saisyou[i * w + j] = -1; TT.push_back({masu[i][j], i, j}); } } rep(i, h * w) { p = TTT[i].size(); rep(j, p - 1) { auto [aa, bb] = TTT[i][j]; auto [cc, dd] = TTT[i][j + 1]; UNI.connect(aa * w + bb, cc * w + dd); } } sort(all(TT)); k = 0; rep(i, h * w) { auto [a, b, c] = TT[i]; saisyou[b * w + c] = max(1ll, k); if (b) { if (masu[b - 1][c] == masu[b][c]) { UNI.connect((b - 1) * w + c, b * w + c); } else if (saisyou[UNI.root((b - 1) * w + c)] > 0) ma(saisyou[UNI.root(b * w + c)], saisyou[UNI.root((b - 1) * w + c)] + 1); } if (c) { if (masu[b][c - 1] == masu[b][c]) { UNI.connect((b)*w + c - 1, b * w + c); } else if (saisyou[UNI.root((b)*w + c - 1)] > 0) ma(saisyou[UNI.root(b * w + c)], saisyou[UNI.root((b)*w + c - 1)] + 1); } if (b < h - 1) { if (masu[b + 1][c] == masu[b][c]) { UNI.connect((b + 1) * w + c, b * w + c); } else if (saisyou[UNI.root((b + 1) * w + c)] > 0) ma(saisyou[UNI.root(b * w + c)], saisyou[UNI.root((b + 1) * w + c)] + 1); } if (c < w - 1) { if (masu[b][c + 1] == masu[b][c]) { UNI.connect((b)*w + c + 1, b * w + c); } else if (saisyou[UNI.root((b)*w + c + 1)] > 0) ma(saisyou[UNI.root(b * w + c)], saisyou[UNI.root((b)*w + c + 1)] + 1); } k = saisyou[UNI.root(b * w + c)]; } rep(i, h) { rep(j, w) { ma(ans, saisyou[i * w + j]); } } print(ans); } int main() { cout << fixed << setprecision(15); cin.tie(0); ios::sync_with_stdio(false); // cin >> codeforces; while (codeforces--) { ans = 0; solve(); } }