#include <stdio.h>
int x, y, minstep, step = 0;
int book[101][101] = {3};
int n, m;
int stx, sty, enx, eny;
int check(int x,int y)
{
if(book[x][y] == 0 &&x >= 0 && x <= n && y >= 0 && y <= m)
{
return 1;
}
return 0;
}
int dfs(int x,int y)
{
int next[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
if(stx == enx && sty == eny)
{
if(step < minstep)
{
minstep = step;
}
return minstep;
}
if(check(x,y))
{
step ++;
int xx, yy;
for(int k = 0; k <= 3; k ++)
{
xx = x + next[k][0];
yy = y + next[k][1];
book[xx][yy] == 1;
dfs(xx,yy);
book[xx][yy] == 0;
}
}
}
int main()
{
scanf("%d %d",&n ,&m);
char map[n][m];
for(int i = 0 ; i < n ;i ++)
{
scanf("%s",map[i]);
}
for(int i = 0 ; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(map[i][j] == '1')
{
book[i][j] == 1;
}
}
}
scanf("%d %d %d %d",&stx, &sty ,&enx, &eny);
dfs( stx , sty);
printf("%d",minstep);
}