#pragma region template #include using namespace std; #include using namespace atcoder; using mint=modint998244353; templateinline bool chmax(T &a,T b){if(ainline bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;} #define rep1(i,a) for(int i=0;i<(int)(a);i++) #define rep2(i,a,b) for(int i =(int)(a);i<(int)(b);i++) #define rep3(i,a,b,c) for(int i=(int)(a);i<(int)(b);i+=(int)(c)) #define overloadRep(a,b,c,d,e,...)e #define rep(...) overloadRep(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__) #define rrep(i,a,b) for(int i=(int)(a);i<=(int)(b);i++) #define drep(i,a,b) for(int i=(int)(a);i>=(int)(b);i--) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define spa " " #define yes "Yes" #define no "No" #define int long long const int inf=8e18; const int dx[4]={0,1,0,-1}; const int dy[4]={1,0,-1,0}; using P=pair; using T=tuple; bool bit(int &x,int &p){return (x>>p)&1;} #pragma endregion signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout<>h>>w; vector>a(h,vector(w));rep(i,h)rep(j,w)cin>>a[i][j]; vector>col(h,vector(w,-1)); col[0][0]=0;col[h-1][w-1]=1; int now=false; priority_queue, greater>q0,q1; vector used0(h,vector(w)); vector used1(h,vector(w)); q0.emplace(a[0][1],0,1);used0[0][1]=true; q0.emplace(a[1][0],1,0);used0[1][0]=true; q1.emplace(a[h-1][w-2],h-1,w-2);used1[h-1][w-2]=true; q1.emplace(a[h-2][w-1],h-2,w-1);used1[h-2][w-1]=true; int ans=0; while(true){ ans++; if(!now){ auto[val,i,j]=q0.top();q0.pop(); col[i][j]=now; rep(d,4){ int ni=i+dx[d],nj=j+dy[d]; if(ni<0 or ni>=h or nj<0 or nj>=w)continue; if(col[ni][nj]==now)continue; if(used0[ni][nj])continue; if(col[ni][nj]==1){cout<=h or nj<0 or nj>=w)continue; if(col[ni][nj]==now)continue; if(col[ni][nj]==0){cout<