#include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) struct Grid { int h; int w; Grid (int h, int w) : h(h), w(w) {} }; struct Walk { int val; int length; Walk (int val, int length) : val(val), length(length) {} }; int main() { int H, W; cin >> H >> W; map> g; rep(h, H) rep(w, W) { int val; cin >> val; g[val].push_back(Grid(h, w)); } vector dh = {0, 1, 0, -1}; vector dw = {1, 0, -1, 0}; vector> dp(H, vector (W, Walk(0, 0))); int length = 0; for (auto it = g.begin(); it != g.end(); it++) { int val = it->first; for (Grid p : it->second) { int len = -1; rep(i, 4) { int nh = p.h + dh[i]; int nw = p.w + dw[i]; if (nh < 0 || nh > H-1 || nw < 0 || nw > W-1) continue; if (dp[nh][nw].val < val) len = max(dp[nh][nw].length, len); } if (len >= 0) { dp[p.h][p.w] = {val, len + 1}; length = max(len + 1, length); } } } cout << length << endl; return 0; }