#include using namespace std; const int maxn = 505; int h, w; int a[maxn][maxn]; int dp[maxn][maxn]; bool isin(int i, int j) { return 0 <= i && i < h && 0 <= j && j < w; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); cin >> h >> w; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } priority_queue> pq; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { pq.push({a[i][j], i * w + j}); } } const int dx[] = {1, 0, -1, 0, 1}; while (pq.size()) { auto [v, idx] = pq.top(); pq.pop(); int i = idx / w, j = idx % w; int mx = 0; for (int r = 0; r < 4; r++) { int ni = i + dx[r], nj = j + dx[r + 1]; if (!isin(ni, nj)) continue; if (v < a[ni][nj]) { mx = max(mx, dp[ni][nj]); } } dp[i][j] = mx + 1; } int ans = 0; for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) { ans = max(ans, dp[i][j]); } cout << ans << "\n"; }