#include<bits/stdc++.h>
using namespace std;
int mp[105][105],n,dx[4] = {0,0,-1,1},dy[4] = {1,-1,0,0},x1,y1,ansx,ansy;
struct zuobiao
{
int x,y;
};
void bfs(int x,int y)
{
queue<zuobiao> q;
q.push({x,y});
mp[x][y] = 1;
while(q.size())
{
auto u = q.front();
q.pop();
for(int i = 0;i < 4;i++)
{
int a = u.x + dx[i],b = u.y + dy[i];
if(mp[a][b] != 0||a > n||b > n||a < 1||b < 1)
{
continue;
}
mp[a][b] = mp[u.x][u.y] + 1;
q.push({a,b});
}
}
}
int main()
{
cin >> n;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
char c;
cin >> c;
if(c == '0')
{
mp[i][j] = 0;
}
else
{
mp[i][j] = 1;
}
}
}
cin >> x1 >> y1 >> ansx >> ansy;
bfs(x1,y1);
cout << mp[ansx][ansy] - 1;
return 0;
}