Exile_Code @ 2023-11-07 22:23:39
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
#include <bitset>
#include <stack>
#include <unordered_set>
#define ull unsigned long long
#define ll long long
#define pii pair<ll ,ll>
#define ft first
#define sd second
#define pr pair
#define cy cout<<"Yes"<<endl
#define cn cout<<"No"<<endl
#define forn(i,n) for(int (i)=0;(i)<(n);(i)++)
#define forne(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define rforn(i,n) for(int (i)=(n-1);(i)>=(0);(i)--)
#define rforne(i,n) for(int (i)=(n);(i)>=(1);(i)--)
#define vv vector
const int inf = 0x004f4f4f;
const int N = 2e5 + 10;
ll t;
struct cmp {
bool operator()(pr<int, pii>& a, pr<int, pii>& b) {
return a.first > b.first;//小根堆
}
};
void solve() {
int n, m; cin >> n >> m;
priority_queue<pr<int, pii>, vector<pr<int, pii>>, cmp >q;
vv<vv<int>>area(n + 10, vv<int>(m + 10));
vv<vv<ll>>dp(n + 10, vv<ll>(m + 10));//多开一圈
forne(i, n) {
forne(j, n) {
cin >> area[i][j];
q.push({ area[i][j],{i,j} });
}
}
ll mx = 0;
while (q.size()) {
auto a = q.top();
q.pop();
int x = a.sd.ft;
int y = a.sd.sd;
if (area[x][y] > area[x + 1][y])
dp[x][y] = max(dp[x][y], dp[x + 1][y]);
if (area[x][y] > area[x - 1][y])
dp[x][y] = max(dp[x][y], dp[x - 1][y]);
if (area[x][y] > area[x][y + 1])
dp[x][y] = max(dp[x][y], dp[x][y + 1]);
if (area[x][y] > area[x][y - 1])
dp[x][y] = max(dp[x][y], dp[x][y - 1]);
dp[x][y]++;
mx = max(mx, dp[x][y]);
}
cout << mx << endl;
}
int main() {
t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}