#include #include #include #include #include template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } template struct CoordComp { std::vector v; bool sorted; CoordComp() : sorted(false) {} int size() { return v.size(); } void add(T x) { v.push_back(x); } void build() { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); sorted = true; } int get_idx(T x) { assert(sorted); return lower_bound(v.begin(), v.end(), x) - v.begin(); } T &operator[](int i) { return v[i]; } }; #define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For(i, 0, n) #define rrep(i, n) rFor(i, n, 0) #define fi first #define se second using namespace std; using lint = long long; using pii = pair; using pll = pair; int dx[2] = {0, 1}; int dy[2] = {1, 0}; int main() { int h, w, n; scanf("%d%d%d", &h, &w, &n); int c[h][w]; vector> tp; rep(i, h) rep(j, w) { scanf("%d", &c[i][j]); tp.emplace_back(c[i][j], i, j); } sort(tp.begin(), tp.end()); int idx[h][w]; rep(i, tp.size()) { auto [_, x, y] = tp[i]; idx[x][y] = i; } vector gr[h * w]; rep(i, tp.size()) { if (i < (int)tp.size() - 1) gr[i].emplace_back(i + 1, 0); auto [_, x, y] = tp[i]; rep(k, 2) { int nx = x + dx[k], ny = y + dy[k]; if (nx < h && ny < w) { if (c[x][y] < c[nx][ny]) gr[i].emplace_back(idx[nx][ny], 1); if (c[x][y] > c[nx][ny]) gr[idx[nx][ny]].emplace_back(i, 1); } } } int dp[h * w]; memset(dp, 0, sizeof(dp)); rep(i, h * w) for (auto [j, w] : gr[i]) chmax(dp[j], dp[i] + w); printf("%d\n", dp[h * w - 1] + 1); }