using namespace std;
int gox[4]={1,-1,0,0},goy[4]={0,0,1,-1},visit[1000010]={0},queue[1000010]={0},maze[1001][1001],b[2],e[2],n,open=1,close=1;
int main()
{ std::ios::sync_with_stdio(false);
cin>>n;char k;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){cin>>k;maze[i][j]=k-'0';}
cin>>b[0]>>b[1]>>e[0]>>e[1];
int x,y;
queue[1]=(b[0]-1)*n+b[1],visit[1]=0;
while(open<=close){
for(int i=0;i<=3;i++){
x=(queue[open]-1)/n+1+gox[i];
if(y%n!=0)y=(queue[open]+1)%n-1+goy[i];
else y=n+goy[i];
if(x==e[0]&&y==e[1]){cout<<visit[open]+1;return 0;}
if((x>=1&&x<=n)&&(y>=1&&y<=n)&&!maze[x][y])queue[++close]=(x-1)*n+y,visit[close]=visit[open]+1;}
open++;}}