#include #include using ll = long long; using ull = unsigned long long; #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++) using namespace std; using namespace atcoder; using mint = modint998244353; const int inf = 1000000007; const ll longinf = 1ll << 60; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int h, w; cin >> h >> w; vector> a(h, vector(w, 0)); rep(i, h) rep(j, w) cin >> a[i][j]; vector> ord; rep(i, h) rep(j, w) ord.push_back({i, j}); sort(ord.begin(), ord.end(), [&](pair x, pair y) { return a[x.first][x.second] < a[y.first][y.second]; }); vector dp(h, vector(w, 1)); vector d = {1, 0, -1, 0, 1}; int ans = 0; for(auto [x, y] : ord) { rep(i, 4) { int nx = x + d[i], ny = y + d[i + 1]; if(0 <= nx && nx < h && 0 <= ny && ny < w && a[nx][ny] < a[x][y]) { dp[x][y] = max(dp[nx][ny] + 1, dp[x][y]); } } ans = max(ans, dp[x][y]); } cout << ans << endl; return 0; }