#include #include #include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) int H,W,N; vector> G; vector> E; vector D; int main(){ cin >> H >> W >> N; G.assign(H,vector(W)); rep(y,H) rep(x,W){ cin >> G[y][x]; G[y][x]--; } E.resize(N); rep(y,H) rep(x,W-1) if(G[y][x] != G[y][x+1]) E[min(G[y][x],G[y][x+1])].push_back(max(G[y][x],G[y][x+1])); rep(y,H-1) rep(x,W) if(G[y][x] != G[y+1][x]) E[min(G[y][x],G[y+1][x])].push_back(max(G[y][x],G[y+1][x])); D.assign(N+1,1); rep(i,N){ for(int e : E[i]) D[e] = max(D[e], D[i] + 1); D[i+1] = max(D[i+1], D[i]); } cout << D.back() << endl; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;