#include<bits/stdc++.h>
using namespace std;
bool a[1010][1010];
struct node
{
int x,y;
int cnt;
};
queue<node> b;
int p[4][2]={
1,0,
0,1,
-1,0,
0,-1,
};
int n,x1,x2,y1,y2;
int main()
{
cin>>n;
string str;
for(int i=1;i<=n;i++)
{
cin>>str;
for(int j=1;j<=n;j++)
{
a[i][j]=str[j-1]-'0';
}
}
cin>>x1>>y1>>x2>>y2;
b.push((node){x1,y1,0});
while(!b.empty())
{
node t=b.front();
b.pop();
if(t.x==x2&&t.y==y2)
{
cout<<t.cnt;
break;
}
for(int i=0;i<4;i++)
{
int x=t.x+p[i][0];
int y=t.y+p[i][1];
if(x>0&&y>0&&x<=n&&y<=n&&!a[x][y])
{
b.push((node){x,y,t.cnt+1});
a[x][y]=1;
}
}
}
}```