/** * author: Sooh * created: 03.09.2021 23:00:58 **/ #include using namespace std; #if __has_include() #include using namespace atcoder; //using mint = modint998244353; //using mint = modint1000000007; #endif #pragma region template using ll = long long; template using V = vector; #define rep(i,n) for(int i=0;i void view(T e){std::cerr << e << std::endl;} template void view(const std::vector& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;} template void view(const std::vector >& vv){cerr << endl;int cnt = 0;for(const auto& v : vv){cerr << cnt << "th : "; view(v); cnt++;} cerr << endl;} ll power(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a;} a = a * a; p >>= 1;} return ret;} ll modpow(ll a, ll p, ll mod){ll ret = 1; while(p){if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;} ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0; while (b) {ll t = a / b ;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;} templatebool chmax(T &a, const K b) { if (abool chmin(T &a, const K b) { if (b topological_sort(vector> &G, vector &indegree){ int V = G.size(); vector ret; queue que; for(int i = 0; i < V; i++){ if(indegree[i] == 0) que.push(i); } while(!que.empty()){ int v = que.front(); que.pop(); for(int nv : G[v]){ indegree[nv]--; if(indegree[nv] == 0) que.push(nv); } ret.push_back(v); } return ret; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); //cout << fixed << setprecision(20); int h,w, n; cin >> h >> w >> n; V> G(n); V> c(h, V(w)); rep(i,h) rep(j,w) cin >> c[i][j], --c[i][j]; V indeg(n); rep(i,h) rep(j,w){ if(i >= 1 && c[i - 1][j] > c[i][j]){ if(!G[c[i][j]].count(c[i - 1][j])){ indeg[c[i - 1][j]]++; G[c[i][j]].insert(c[i - 1][j]); } } if(j >= 1 && c[i][j - 1] > c[i][j]){ if(!G[c[i][j]].count(c[i][j - 1])){ indeg[c[i][j - 1]]++; G[c[i][j]].insert(c[i][j - 1]); } } if(i < h - 1 && c[i + 1][j] > c[i][j]){ if(!G[c[i][j]].count(c[i + 1][j])){ indeg[c[i + 1][j]]++; G[c[i][j]].insert(c[i + 1][j]); } } if(j < w - 1 && c[i][j + 1] > c[i][j]){ if(!G[c[i][j]].count(c[i][j + 1])){ indeg[c[i][j + 1]]++; G[c[i][j]].insert(c[i][j + 1]); } } } auto res = topological_sort(G, indeg); V dp(n + 1, 0); for(int v : res){ if(dp[v] == 0) dp[v] = 1; for(int nv : G[v]){ chmax(dp[nv], dp[v] + 1); } } cout << *max_element(all(dp)) << endl; }