#include using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x=0&&yvoid pr(const A &a,const B&...b){cout<void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")< P; ll n,m; string s[2222]; vector > bfs(int sx,int sy) { vector > d(n,vector(m,MAX)); d[sx][sy]=0; queue

que; que.push(P(sx,sy)); while(!que.empty()) { P p=que.front();que.pop(); ll x=p.F,y=p.S; rep(i,4) { ll xx=x+dx[i],yy=y+dy[i]; if(check(n,m,xx,yy)&&s[xx][yy]=='.'&&d[xx][yy]==MAX) { d[xx][yy]=d[x][y]+1; que.push(P(xx,yy)); } } } return d; } void Main() { ll sx,sy,tx,ty; cin >> n >> m >> sx >> sy >> tx >> ty; rep(i,n) cin >> s[i]; sx--,sy--,tx--,ty--; vector > c=bfs(sx,sy); vector > d=bfs(tx,ty); ll ans=MAX; rep(i,n) { rep(j,m) { if(s[i][j]=='.') { rep(l,4) { vector

v; rep(k,3) { int x=i+dx[(k+l)%4],y=j+dy[(k+l)%4]; if(check(n,m,x,y)&&s[x][y]=='.') v.pb(P(x,y)); } if(v.size()!=3) continue; ll M=MAX; vector

k1,k2; rep(k,3) { ll dd=c[v[k].F][v[k].S]; if(dd=2) k1.clear(); if(k2.size()>=2) k2.clear(); do { if(k1.size()&&v[0]==k1[0]) continue; if(k2.size()&&v[1]==k2[0]) continue; ans=min(ans,c[v[0].F][v[0].S]+d[v[0].F][v[0].S]+c[v[1].F][v[1].S]+d[v[1].F][v[1].S]); } while(next_permutation(all(v))); } } } } if(ans==MAX) ans=-1; pr(ans); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}