#include using namespace std; int main(){ int h,w;cin>>h>>w; vector> a(h,vector(w)); unordered_map> mp; for(int i=0;i>a[i][j]; mp[a[i][j]]={i,j}; } unordered_set st0,st1; priority_queue,greater> pq1,pq0; vector dx={1,0,-1,0},dy={0,1,0,-1}; int idx=1; pq1.push(a[0][0]); pq0.push(a[h-1][w-1]); while(!pq1.empty()||!pq0.empty()){ if((idx&1)==0){ idx++; while(!pq0.empty()&&st0.count(pq0.top()))pq0.pop(); if(pq0.empty()){ continue; } auto x=pq0.top();pq0.pop(); st0.insert(x); for(int i=0;i<4;i++){ if(mp[x].first+dx[i]<0||mp[x].first+dx[i]>=h)continue; if(mp[x].second+dy[i]<0||mp[x].second+dy[i]>=w)continue; if(st0.count(a[mp[x].first+dx[i]][mp[x].second+dy[i]]))continue; if(st1.count(a[mp[x].first+dx[i]][mp[x].second+dy[i]])){ cout<=h)continue; if(mp[x].second+dy[i]<0||mp[x].second+dy[i]>=w)continue; if(st1.count(a[mp[x].first+dx[i]][mp[x].second+dy[i]]))continue; if(st0.count(a[mp[x].first+dx[i]][mp[x].second+dy[i]])){ cout<