// #pragma GCC optimize("O3,unroll-loops") #include // #include using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); static const vector> dr4{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int h, w; cin >> h >> w; vector> a(h, vector(w)); for(auto &x: a | ranges::views::join){ cin >> x; } vector> order; for(auto i = 0; i < h; ++ i){ for(auto j = 0; j < w; ++ j){ order.push_back({i, j}); } } ranges::sort(order, [&](auto x, auto y){ return a[x[0]][x[1]] < a[y[0]][y[1]]; }); vector> dp(h, vector(w, 1)); for(auto [x, y]: order){ for(auto [dx, dy]: dr4){ int xn = x + dx, yn = y + dy; if(0 <= min(xn, yn) && xn < h && yn < w && a[x][y] < a[xn][yn]){ dp[xn][yn] = max(dp[xn][yn], dp[x][y] + 1); } } } cout << ranges::max(dp | ranges::views::join) << "\n"; return 0; } /* */