#include using namespace std; int main(){ int h, w; cin >> h >> w; vector> a(h, vector(w)); for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ cin >> a[i][j]; } } vector> g(h*w); vector> gr(h*w); int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ for(int k = 0;k < 4;k++){ int nx = i + dx[k], ny = j + dy[k]; if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if(a[i][j] < a[nx][ny]){ g[i*w+j].push_back(nx*w+ny); } } } } priority_queue, vector>> que; vector dist(h*w, 1); for(int i = 0;i < h*w;i++){ que.push(make_tuple(1, a[i/w][i%w], i)); } while(!que.empty()){ auto [d, sc, v] = que.top(); que.pop(); if(dist[v] > d) continue; for(auto to: g[v]){ if(dist[to] < dist[v] + 1){ dist[to] = dist[v] + 1; que.push(make_tuple(dist[to], a[to/w][to%w], to)); } } } long long ans = 0; for(int i = 0;i < h*w;i++){ ans = max(ans, dist[i]); } cout << ans << endl; }