#include using namespace std; using pi=pair; using pip=pair; int dx4[4]={1,-1,0,0}; int dy4[4]={0,0,1,-1}; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int h,w; cin >> h >> w; vector> a(h,vector(w)); for(int i=0;i> a[i][j]; } } vector> col(h,vector(w,-1)); vector> fl(h,vector(w,0)); col[0][0]=1; fl[0][0]=3; col[h-1][w-1]=0; fl[h-1][w-1]=3; vector,greater>> pq(2); pq[1].push({a[0][1],{0,1}}); fl[0][1]|=2; pq[1].push({a[1][0],{1,0}}); fl[1][0]|=2; pq[0].push({a[h-1][w-2],{h-1,w-2}}); fl[h-1][w-2]|=1; pq[0].push({a[h-2][w-1],{h-2,w-1}}); fl[h-2][w-1]|=1; for(int att=1;;att++){ pip od=pq[att%2].top(); pq[att%2].pop(); int x=od.second.first; int y=od.second.second; col[x][y]=(att%2); for(int k=0;k<4;k++){ int nx=x+dx4[k]; int ny=y+dy4[k]; if(!(0<=nx && nx