#include #include using namespace std; using namespace atcoder; using lint = long long; using ulint = unsigned long long; using llint = __int128_t; struct edge; using graph = vector>; #define endl '\n' constexpr int INF = 1<<30; constexpr lint INF64 = 1LL<<61; constexpr lint mod107 = 1e9+7; using mint107 = modint1000000007; constexpr long mod = 998244353; using mint = modint998244353; lint ceilDiv(lint x, lint y){if(x >= 0){return (x+y-1)/y;}else{return x/y;}} lint floorDiv(lint x, lint y){if(x >= 0){return x/y;}else{return (x-y+1)/y;}} lint Sqrt(lint x) {assert(x >= 0); lint ans = sqrt(x); while(ans*ans > x)ans--; while((ans+1)*(ans+1)<=x)ans++; return ans;} lint gcd(lint a,lint b){if(a&v){lint ans = INF64;for(lint i:v){ans = min(ans, i);}return ans;} lint chmax(vector&v){lint ans = -INF64;for(lint i:v){ans = max(ans, i);}return ans;} double dist(double x1, double y1, double x2, double y2){return sqrt(pow(x1-x2, 2) + pow(y1-y2,2));} string toString(lint n){string ans = "";if(n == 0){ans += "0";}else{while(n > 0){int a = n%10;char b = '0' + a;string c = "";c += b;n /= 10;ans = c + ans;}}return ans;} string toString(lint n, lint k){string ans = toString(n);string tmp = "";while(ans.length() + tmp.length() < k){tmp += "0";}return tmp + ans;} vectorprime;void makePrime(lint n){prime.push_back(2);for(lint i=3;i<=n;i+=2){bool chk = true;for(lint j=0;j>19))^(t^(t>>8)) ); } struct Point { lint x, y; int quad; Point(lint X, lint Y) { x = X; y = Y; quad = getQuadrant(); } int getQuadrant() { if(x >= 0) { if(y >= 0) return 1; else return 4; } else { if(y >= 0) return 2; else return 3; } } }; bool operator<(const Point &left, const Point &right) { if(left.quad == right.quad) { return left.y * right.x < left.x * right.y; } else { return left.quad < right.quad; } } struct Frac { lint upper, lower; Frac() { Frac(0,1); } Frac(lint u, lint l) { assert(l != 0); if(u <= 0 && l < 0) { upper = -u; lower = -l; } else { upper = u; lower = l; } reduction(); } Frac(lint u) { upper = u; lower = 1; } void reduction() { if(upper != 0) { lint g = gcd(abs(upper), abs(lower)); upper /= g; lower /= g; if(lower < 0) { lower *= -1; upper *= -1; } } else { lower = 1; } } Frac operator+(const Frac &other) { lint L = lower * other.lower; lint U = upper*other.lower + lower*other.upper; return Frac(U, L); } Frac operator-(const Frac &other) { lint L = lower * other.lower; lint U = upper*other.lower - lower*other.upper; upper = U; lower = L; return Frac(U, L); } bool operator<=(const Frac &other) { return upper*other.lower <= lower*other.upper; } Frac operator*(const Frac &other) { lint L = lower * other.lower; lint U = upper * other.upper; return Frac(U, L); } Frac operator/(const Frac &other) { assert(other.upper != 0); lint L = lower * other.upper; lint U = upper * other.lower; return Frac(U, L); } }; bool operator<(const Frac &left, const Frac &right) { return left.upper*right.lower < left.lower*right.upper; } lint extGCD(lint a, lint b, lint &x, lint &y) { if (b == 0) { x = 1; y = 0; return a; } lint d = extGCD(b, a%b, y, x); y -= a/b * x; return d; } struct edge{ lint to; lint cost; }; vectordijkstra(int s, graph &g) { vecret(g.size(), INF64); priority_queue>que; que.push({-0, s}); ret[s] = 0; while(!que.empty()) { auto q = que.top(); que.pop(); for(auto e: g[q.second]) { if(ret[e.to] > -q.first + e.cost) { ret[e.to] = -q.first + e.cost; que.push({-ret[e.to], e.to}); } } } return ret; } int main(){ lint h,w; cin >> h >> w; lint a[h][w]; rep(i, h) rep(j, w) { cin >> a[i][j]; } vec>>q(2); vec>went(h, vec(w, -1)); q[0].push({-a[1][0], 1, 0}); q[0].push({-a[0][1], 0, 1}); q[1].push({-a[h-1][w-2], h-1, w-2}); q[1].push({-a[h-2][w-1], h-2, w-1}); went[0][0] = 0; went[h-1][w-1] = 1; vecdx = {1,-1,0,0}; vecdy = {0,0,1,-1}; rep(i, h*w) { int chk = 0; while(!q[i%2].empty()) { auto qq = q[i%2].top(); q[i%2].pop(); int x = qq[1], y = qq[2]; if(went[x][y]!= -1) continue; went[x][y] = i%2; chk = 1; rep(d, 4) { int nx = x+dx[d]; int ny = y+dy[d]; if(nx >= 0 && ny >= 0 && nx < h && ny < w) { if(went[nx][ny] == (i+1)%2) { goto fin; } if(went[nx][ny] == -1)q[i%2].push({-a[nx][ny], nx, ny}); } } break; } if(!chk) { fin: cout << i+chk << endl; return 0; } } }