#pragma GCC target("avx2") #pragma GCC optimize("Ofast,unroll-loops") #include #include #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define ALL(a) a.begin(), a.end() #undef long #define long long long #define ll long #define vec vector using namespace std; using mint = atcoder::modint; ostream &operator<<(ostream &os, mint a) { return os << a.val(); } template ostream &operator<<(ostream &os, vector &a) { const int n = a.size(); rep(i, n) { os << a[i]; if (i + 1 != n) os << " "; } return os; } template ostream &operator<<(ostream &os, array &a) { rep(i, n) os << a[i] << " \n"[i + 1 == n]; return os; } template istream &operator>>(istream &is, vector &a) { for (T &i : a) is >> i; return is; } template bool chmin(T &x, T y) { if (x > y) { x = y; return true; } return false; } template bool chmax(T &x, T y) { if (x < y) { x = y; return true; } return false; } void solve() { int h, w; cin >> h >> w; vec> a(h, vec(w)); rep(i, h) rep(j, w) cin >> a[i][j]; vec> g(h * w); constexpr int dx[] = {0, 1, 0, -1}; constexpr int dy[] = {1, 0, -1, 0}; rep(i, h) rep(j, w) { rep(k, 4) { int nx = i + dx[k]; int ny = j + dy[k]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (a[i][j] < a[nx][ny]) g[i * w + j].push_back(nx * w + ny); } } queue q; vec d(h * w); rep(i, h * w) for (int j : g[i]) d[j]++; rep(i, h * w) if (d[i] == 0) q.push(i); vec dp(h * w); while (!q.empty()) { const int v = q.front(); q.pop(); for (int u : g[v]) { chmax(dp[u], dp[v] + 1); if (--d[u] == 0) q.push(u); } } cout << *max_element(ALL(dp)) + 1 << endl; } int main() { // srand((unsigned)time(NULL)); cin.tie(nullptr); ios::sync_with_stdio(false); // cout << fixed << setprecision(40); int t = 1; // cin >> t; while (t--) solve(); return 0; }