#include <fstream>
# include <queue>
using namespace std;
ifstream f1("inginer.in");
ofstream f2("inginer.out");
struct punct {int l,c;}p,pnou;
queue <punct> coada;
int A[50][50],i,n,m,x1,x2,y1,y2,j;
int dl[]={-1,0,1,0},dc[]={0,1,0,-1};
char x;
void citeste()
{
f1>>n>>m;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
f1>>x;
if(x=='X')
A[j]=-1;
else
A[j]=0;
}
//bordam cu 0;
for(i=1;i<=n;i++)
A[0]=A[n+1]=A
- =A[n+1]=0;
}
void lee()
{f1>>x1>>y1>>x2>>y2;p.l=x1;p.c=y1;A[p.l][p.c]=1;
A[x2][y2]=0;
coada.push(p);
A[x1][y1]=1;
punct pnou;
while(!coada.empty() && A[x2][y2]==0)
{
p=coada.front();
coada.pop();
for(i=0;i<4;i++)
{pnou.l=p.l+dl;
pnou.c=p.c+dc;
if(A[pnou.l][pnou.c]==0)
{
coada.push(pnou);
A[pnou.l][pnou.c]=A[p.l][p.c]+1;
}
}
}
f2<<A[x2][y2];
}
int main()
{citeste();
while(x1!=0 && y1!=0 && x2!=0 && y2!=0);
{
lee();
}
f1.close();f2.close();
return 0;
}