#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int H,W; cin >> H >> W; vector> X; vector> A(H,vector(W)); for(int i=0; i> a; A.at(i).at(k) = a; X.push_back({a,i,k}); } sort(X.rbegin(),X.rend()); vector dist(H,vector(W)); vector dx = {-1,0,1,0},dy = {0,1,0,-1}; for(auto [a,x,y] : X){ int now = dist.at(x).at(y); for(int i=0; i<4; i++){ int nx = x+dx.at(i),ny = y+dy.at(i); if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue; if(A.at(nx).at(ny) >= a) continue; dist.at(nx).at(ny) = max(dist.at(nx).at(ny),now+1); } } int answer = 0; for(auto &h : dist) for(auto &w : h) answer = max(answer,w); cout << answer+1 << endl; }