#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; setst0,st1; vector used0(h,vector(w)); vector used1(h,vector(w)); st0.emplace(a[0][1],0,1);used0[0][1]=true; st0.emplace(a[1][0],1,0);used0[1][0]=true; st1.emplace(a[h-1][w-2],h-1,w-2);used1[h-1][w-2]=true; st1.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]=*st0.begin(); cout<=h or nj<0 or nj>=w)continue; if(col[ni][nj]==now)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<