#include <bits/stdc++.h>
using namespace std;
int n,c[1000010][3];
char a[2010][1010];
int fx[5]={0,0,1,0,-1};
int fy[5]={0,1,0,-1,0};
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
int xx,yy,sx,sy;
cin>>xx>>yy>>sx>>sy;
int i=1,t=1;
c[1][0]=xx;
c[1][1]=yy;
c[1][2]=0;
while(i<t||t==1)
{
int x=c[i][0];
int y=c[i][1];
int k=c[i][2];
a[x][y]='1';
if(x==sx&&y==sy)
{
cout<<k;
return 0;
}
for(int q=1;q<=4;q++)
{
int tx=x+fx[q];
int ty=y+fy[q];
if(a[tx][ty]=='0');
{
c[++t][0]=tx,c[t][1]=ty;
c[t][2]=k+1;
}
}
i++;
}
}