#include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; template using V = vector; template using VV = V>; template using VVV = V>; template using VVVV = VV>; #define rep(i,n) for(ll i=0ll;i bool chmin(T& t, const U& u) { if (t > u){ t = u; return 1;} return 0; } template bool chmax(T& t, const U& u) { if (t < u){ t = u; return 1;} return 0; } // cin.tie(nullptr); // ios::sync_with_stdio(false); // cout << fixed << setprecision(20); void solve(){ ll h,w; cin >> h >> w; VV v(h+2, V(w+2, INF)); rep(i,h) rep(j,w) cin >> v[i+1][j+1]; V> ww; rep(i,h) rep(j,w) ww.eb(v[i+1][j+1], i+1, j+1); sort(be(ww)); VV ans(h+2, V(w+2, 0)); V dx={0,-1,0,1}, dy={1,0,-1,0}; for(auto[d,x,y]:ww) rep(i,4){ ll nx = x+dx[i]; ll ny = y+dy[i]; if(d > v[nx][ny]) chmax(ans[x][y], ans[nx][ny]+1); } ll a = 0; all(vv, ans) all(va, vv) chmax(a, va); cout << a << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int t=1; // cin >> t; rep(i,t) solve(); }