int h,w,ra,ca,rb,cb; char s[1333][1335]; int dp1[1333][1333]; int dp2[1333][1333][2]; struct{int y,x,z,d} q[16000000]; int qr,qw; void enq(int y,int x,int z,int d){ q[qw].y=y; q[qw].x=x; q[qw].z=z; q[qw].d=d; ++qw; } void f1(){ qr=qw=0; enq(ra,ca,0,1); while(qr=0){ enq(y,x,1,d+1); } enq(y-1,x,z,d+1); enq(y+1,x,z,d+1); enq(y,x-1,z,d+1); enq(y,x+1,z,d+1); } } } int f4(int r,int c,int vy,int vx){ for(int y=0;y0&&d2>d+d1+2)d2=d+d1+2; d=f4(ra,ca,+1,0);if(d>0&&d2>d+d1+2)d2=d+d1+2; d=f4(ra,ca,0,-1);if(d>0&&d2>d+d1+2)d2=d+d1+2; d=f4(ra,ca,0,+1);if(d>0&&d2>d+d1+2)d2=d+d1+2; d=f4(rb,cb,-1,0);if(d>0&&d2>d+d1+2)d2=d+d1+2; d=f4(rb,cb,+1,0);if(d>0&&d2>d+d1+2)d2=d+d1+2; d=f4(rb,cb,0,-1);if(d>0&&d2>d+d1+2)d2=d+d1+2; d=f4(rb,cb,0,+1);if(d>0&&d2>d+d1+2)d2=d+d1+2; } if(d2==3000000){ puts("-1"); exit(0); } printf("%d",d1+d2); }