#include using namespace std; struct cell { int x, y; }; const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; int main() { int H, W; cin >> H >> W; vector A(H, vector(W, 0)); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { cin >> A[i][j]; } } auto compare = [&](cell a, cell b) { return !(A[a.x][a.y] < A[b.x][b.y]); }; priority_queue, decltype(compare)> pq{compare}; for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) pq.push({i, j}); vector dp(H, vector(W, 1)); while(!pq.empty()) { cell C = pq.top(); pq.pop(); for(int dir = 0; dir < 4; dir++) { int nx(C.x+dx[dir]), ny(C.y+dy[dir]); if(0 <= nx && nx < H && 0 <= ny && ny < W) { if(A[C.x][C.y] < A[nx][ny]) dp[nx][ny] = max(dp[C.x][C.y]+1, dp[nx][ny]); } } } int ans(0); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { ans = max(dp[i][j], ans); } } cout << ans << endl; return 0; }