#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 1e3+5;
int n,ans,sx,sy,ex,ey,maps[N][N];
int dx[5] = {0,0,1,-1},
dy[5] = {1,-1,0,0};
bool b[N][N];
struct point
{
int x,y;
};
queue<point> q;
int bfs()
{
q.push({sx-1,sy-1});
b[sx-1][sy-1] = 1;
while(!q.empty())
{
point p;
p = q.front();
q.pop();
if(p.x == ex-1 and p.y == ey-1) return maps[p.x][p.y];
for(int i = 0;i<4;i++)
{
int xx = p.x+dx[i],yy = p.y+dy[i];
if(xx<0 or xx>=n or yy<0 or yy>=n or b[xx][yy] or maps[yy]) continue;
b[xx][yy] = 1;
maps[xx][yy] = maps[p.x][p.y] + 1;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
memset(maps,0,sizeof(maps));
cin>>n;
for(int i = 0;i<n;i++)
for(int j = 0;j<n;j++)
{
char c;
cin>>c;
maps[i][j] = c-'0';
}
cin>>sx>>sy>>ex>>ey;
cout<<bfs()<<endl;
return 0;
}