#include #include #include using namespace atcoder; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000 int H,W; vector S; vector dy = {0,0,1,-1},dx = {1,-1,0,0}; vector> get(int r,int c){ queue> Q; vector> dis(H,vector(W,Inf)); dis[r][c] = 0; Q.emplace(r,c); while(Q.size()>0){ int y = Q.front().first,x = Q.front().second; Q.pop(); rep(i,4){ int yy = y+dy[i],xx = x+dx[i]; if(yy<0||yy>=H||xx<0||xx>=W)continue; if(S[yy][xx]=='#')continue; if(dis[yy][xx]!=Inf)continue; dis[yy][xx] = dis[y][x]+1; Q.emplace(yy,xx); } } return dis; } int main(){ cin>>H>>W; S.resize(H); int r0,c0,r1,c1; cin>>r0>>c0>>r1>>c1; r0--,c0--,r1--,c1--; rep(i,H){ cin>>S[i]; } auto d0 = get(r0,c0),d1 = get(r1,c1); if(d0[r1][c1]==Inf)cout<<-1<D+1){ cout<<2*D< G(2*H*W); rep(i,H){ rep(j,W){ if(S[i][j]=='#')continue; G.add_edge(2*(i*W+j),2*(i*W+j)+1,1,0); rep(k,4){ int ii = i,jj = j; ii += dy[k],jj += dx[k]; if(S[ii][jj]=='#')continue; G.add_edge(2*(i*W+j)+1,2*(ii*W+jj),1,1); } } } cout<<-1<