#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int h,w; cin >> h >> w; vector> a(h, vector(w)); vector>> v; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> a[i][j]; v.push_back({a[i][j], {i, j}}); } } sort(v.begin(), v.end()); vector> d(h, vector(w, -1)); for (int i = 0; i < v.size(); ++i) { auto [x, y] = v[i].second; d[x][y] = 0; if (x and a[x-1][y] < a[x][y]) { d[x][y] = max(d[x][y], d[x-1][y]+1); } if (x+1 < h and a[x+1][y] < a[x][y]) { d[x][y] = max(d[x][y], d[x+1][y]+1); } if (y and a[x][y-1] < a[x][y]) { d[x][y] = max(d[x][y], d[x][y-1]+1); } if (y+1 < w and a[x][y+1] < a[x][y]) { d[x][y] = max(d[x][y], d[x][y+1]+1); } } int res = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { res = max(res, d[i][j]); } } cout << res+1 << endl; }