#include using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i=0;i--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int H,W; cin>>H>>W; vector A(H,vector(W)); for(int i=0;i>A[i][j]; } } set>s0,s1; s0.insert(make_tuple(A[0][1],0,1)); s0.insert(make_tuple(A[1][0],1,0)); s1.insert(make_tuple(A[H-1][W-2],H-1,W-2)); s1.insert(make_tuple(A[H-2][W-1],H-2,W-1)); vector F(H,vector(W,-1)); F[0][0]=0; F[H-1][W-1]=1; for(int t=0;;t++){ if(t%2==0){ if(s0.empty()){ cout<