#include <algorithm>
#include <cstdio>
using namespace std;
int a[1001][1001];
int check[1001][1001];
int n,flag,i,j,k,nowx,nowy,head,tail,c,enx,eny,stx,sty;
struct point{
int x;
int y;
int s;
};
point que[1000001];
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int main()
{
scanf("%d",&n);
getchar();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
c=getchar();
a[i][j]=c-48;
}
getchar();
}
scanf("%d%d",&stx,&sty);
scanf("%d%d",&enx,&eny);
head=1;
tail=1;
flag=0;
que[tail].s=0;
que[tail].x=stx;
que[tail].y=sty;
check[stx][sty]=1;
++tail;
while (head<tail)
{
for(k=0;k<=3;k++)
{
nowx=que[head].x+next[k][0];
nowy=que[head].y+next[k][1];
if(nowx<1 || nowy<1 || nowx>n || nowy>n)
continue;
if(check[nowx][nowy]==0 && a[nowx][nowy]==0)
{
que[tail].s=que[head].s+1;
que[tail].x=nowx;
que[tail].y=nowy;
check[nowx][nowy]=1;
++tail;
}
if (nowx==enx && nowy==eny)
{
flag=-1;
break;
}
}
if (flag==-1) break;
++head;
}
printf("%d",que[tail-1].s);
return 0;
}