#include using namespace std; #define lli long long int #define uli unsigned long long int #define INF 99999999999 #define rep(i,m,n) for(lli i = m;i < n;i++) #define rrep(i,m,n) for(lli i=m-1;i>=n;i--) #define pb(n) push_back(n) #define UE(N) N.erase(unique(N.begin(),N.end()),N.end()); #define Sort(n) sort(n.begin(), n.end()) #define Rev(n) reverse(n.begin(),n.end()) #define Out(S) cout << S << endl #define NeOut(S) cout << S #define HpOut(S) cout << setprecision(20) << S << endl #define Vecpr(K,L1,L2,N) vector> K(N) #define Vec(K,L,N,S) vector K(N,S) #define DV(K,L,N,M,R) vector> K(N, vector(M,R)) #define pint pair #define Lower(v,X) lower_bound(v.begin(),v.end(),X)-v.begin(); #define mod 1000000007 #define MAX 5100000 #define chmax(a, b) a = (((a)<(b)) ? (b) : (a)) #define chmin(a, b) a = (((a)>(b)) ? (b) : (a)) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main(){ lli A,B,C,L,R,N,M,K,X=0,Y=0,W,H=INF,sum=0,num=0,flag=0;string S,T,O; cin >> W >> H >> A >> B >> L >> R; DV(P,char,W,H,'.'); rep(i,0,W)rep(j,0,H)cin >> P[i][j]; DV(dist,int,W,H,-1); dist[A-1][B-1]=0; queue que; que.push(pint(A-1,B-1)); while(!que.empty()){ auto Xq=que.front();que.pop(); rep(i,0,4){ lli nx=Xq.first+dx[i]; lli ny=Xq.second+dy[i]; if(P[nx][ny]=='#'||dist[nx][ny]!=-1)continue; dist[nx][ny]=dist[Xq.first][Xq.second]+1; que.push(pint(nx,ny)); } } Out(dist[L-1][R-1]); }