#include using namespace std; int main(){ int h, w; cin >> h >> w; const auto id = [&](int x, int y)->int { return x * w + y; }; vector a(h, vector(w)); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> a[i][j]; } } vector graph(h * w, vector(0)); vector in(h * w, 0); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(i != 0){ if(a[i][j] < a[i - 1][j]){ graph[id(i, j)].emplace_back(id(i - 1, j)); in[id(i - 1, j)]++; } if(a[i][j] > a[i - 1][j]){ graph[id(i - 1, j)].emplace_back(id(i, j)); in[id(i, j)]++; } } if(j != 0){ if(a[i][j] < a[i][j - 1]){ graph[id(i, j)].emplace_back(id(i, j - 1)); in[id(i, j - 1)]++; } if(a[i][j] > a[i][j - 1]){ graph[id(i, j - 1)].emplace_back(id(i, j)); in[id(i, j)]++; } } } } queue que; vector depth(h * w, 1); for(int i = 0; i < h * w; i++){ if(in[i] == 0) que.push(i); } while(que.size()){ int p = que.front(); que.pop(); for(int to : graph[p]){ in[to]--; depth[to] = max(depth[to], depth[p] + 1); if(in[to] == 0) que.push(to); } } cout << *max_element(depth.begin(), depth.end()) << endl; }