#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #if __has_include() #include #endif #define GET_MACRO(_1, _2, _3, NAME, ...) NAME #define _rep(i, n) _rep2(i, 0, n) #define _rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define UNIQUE(x) \ std::sort((x).begin(), (x).end()); \ (x).erase(std::unique((x).begin(), (x).end()), (x).end()) using i64 = long long; template bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; } template bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; } template inline void YesNo(bool f = 0, const T yes = "Yes", const U no = "No") { if (f) std::cout << yes << "\n"; else std::cout << no << "\n"; } namespace io { template istream& operator>>(istream& i, pair& p) { i >> p.first >> p.second; return i; } template istream& operator>>(istream& i, vector& v) { rep(j, v.size()) i >> v[j]; return i; } template string join(vector& v) { stringstream s; rep(i, v.size()) s << ' ' << v[i]; return s.str().substr(1); } template ostream& operator<<(ostream& o, vector& v) { if (v.size()) o << join(v); return o; } template string join(vector>& vv) { string s = "\n"; rep(i, vv.size()) s += join(vv[i]) + "\n"; return s; } template ostream& operator<<(ostream& o, vector>& vv) { if (vv.size()) o << join(vv); return o; } template ostream& operator<<(ostream& o, pair& p) { o << p.first << " " << p.second; return o; } void OUT() { std::cout << "\n"; } template void OUT(Head&& head, Tail&&... tail) { std::cout << head; if (sizeof...(tail)) std::cout << ' '; OUT(std::forward(tail)...); } void OUTL() { std::cout << std::endl; } template void OUTL(Head&& head, Tail&&... tail) { std::cout << head; if (sizeof...(tail)) std::cout << ' '; OUTL(std::forward(tail)...); } void IN() {} template void IN(Head&& head, Tail&&... tail) { cin >> head; IN(std::forward(tail)...); } } // namespace io using namespace io; namespace useful { long long modpow(long long a, long long b, long long mod) { long long res = 1; while (b) { if (b & 1) res *= a, res %= mod; a *= a; a %= mod; b >>= 1; } return res; } bool is_pow2(long long x) { return x > 0 && (x & (x - 1)) == 0; } template void rearrange(vector& a, vector& p) { vector b = a; for (int i = 0; i < int(a.size()); i++) { a[i] = b[p[i]]; } return; } template vector> rle_sequence(vector& a) { vector> res; int n = a.size(); int l = 1; rep(i, n - 1) { if (a[i] == a[i + 1]) l++; else { res.emplace_back(a[i], l); l = 1; } } res.emplace_back(a.back(), l); return res; } vector> rle_string(string a) { vector> res; int n = a.size(); if (n == 1) return vector>{{a[0], 1}}; int l = 1; rep(i, n - 1) { if (a[i] == a[i + 1]) l++; else { res.emplace_back(a[i], l); l = 1; } } res.emplace_back(a.back(), l); return res; } vector linear_sieve(int n) { vector primes; vector res(n + 1); iota(all(res), 0); for (int i = 2; i <= n; i++) { if (res[i] == i) primes.emplace_back(i); for (auto j : primes) { if (j * i > n) break; res[j * i] = j; } } return res; // return primes; } template vector dijkstra(vector>>& graph, int start) { int n = graph.size(); vector res(n, 2e18); res[start] = 0; priority_queue, vector>, greater>> que; que.push({0, start}); while (!que.empty()) { auto [c, v] = que.top(); que.pop(); if (res[v] < c) continue; for (auto [nxt, cost] : graph[v]) { auto x = c + cost; if (x < res[nxt]) { res[nxt] = x; que.push({x, nxt}); } } } return res; } } // namespace useful using namespace useful; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; IN(h, w); vector a(h, vector(w)); IN(a); auto gtoi = [&](int i, int j) { return i * w + j; }; auto itog = [&](int x) { return make_pair(x / w, x % w); }; using P = pair; priority_queue, greater

> o, z; o.emplace(a[0][1], gtoi(0, 1)); o.emplace(a[1][0], gtoi(0, 1)); z.emplace(a[h - 1][w - 2], gtoi(h - 1, w - 2)); z.emplace(a[h - 2][w - 1], gtoi(h - 2, w - 1)); vector v(h, vector(w, -1)); v[0][0] = 1; v[h - 1][w - 1] = 0; vector di = {-1, 0, 0, 1}; vector dj = {0, -1, 1, 0}; auto check = [&](int i, int j) { return 0 <= i && i < h && 0 <= j && j < w; }; rep(c, 1, h * w + 1) { int i, j; if (c & 1) { auto [d, p] = o.top(); tie(i, j) = itog(p); o.pop(); } else { auto [d, p] = z.top(); tie(i, j) = itog(p); z.pop(); } if (v[i][j] >= 0) { c--; continue; } v[i][j] = c & 1; rep(k, 4) { int ni = i + di[k], nj = j + dj[k]; if (!check(ni, nj)) continue; if (v[ni][nj] >= 0 && (v[i][j] != v[ni][nj])) { OUT(c); return 0; } else if (v[ni][nj] == -1) { v[ni][nj] = -2; if (c & 1) { o.emplace(a[ni][nj], gtoi(ni, nj)); } else { z.emplace(a[ni][nj], gtoi(ni, nj)); } } } } }