#include using namespace std; using ll = long long; using P = pair; using T = tuple; #include using namespace atcoder; // using mint = modint998244353; // using mint = modint1000000007; #define rep(i, n) for(ll i = 0; i < n; i++) #define reps(i, l, r) for(ll i = l; i < r; i++) // 左上右下の順番 vector dx = {0,-1,0,1}; vector dy = {-1,0,1,0}; inline bool outField(int x,int y,int h,int w){ if(0 <= x && x < h && 0 <= y && y < w)return false; return true; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); ll h, w; cin >> h >> w; vector> a(h,vector(w)), walk(h,vector(w,-1)); rep(i,h) rep(j,w) cin >> a[i][j]; auto dfs = [&](auto self, ll x, ll y) -> void { if( walk[x][y] != -1 ) return; ll res = 0; rep(d,4) { ll nx = x + dx[d], ny = y + dy[d]; if(outField(nx,ny,h,w)) continue; if(a[x][y] < a[nx][ny]) { self(self, nx, ny); res = max(res, walk[nx][ny]); } } walk[x][y] = res + 1; return; }; ll ans = 0; rep(i,h) rep(j,w) { dfs(dfs, i, j); ans = max(ans, walk[i][j]); } cout << ans << '\n'; return 0; }