#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <math.h>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef double dl;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
string s[1005];
bool book[1005][1005];
int n,fx[4][2]={0,1,1,0,0,-1,-1,0},x1,x2,y2,qy,ans;
struct node{
int x;
int y;
int d;
};
queue<node> q;
void bfs()
{
q.push({x1,qy,0});
book[x1][qy]=1;
while(!q.empty())
{
node t=q.front();
q.pop() ;
for(int i = 0;i <4;i++)
{
int xx=t.x +fx[i][0];
int yy=t.y +fx[i][1];
if (xx<0||yy<0||xx>=n||yy>=n) continue;
if(xx>=0&&yy>=0&&xx<n&&yy<n&&s[xx][yy]=='0'&&book[xx][yy]==0)
{
if(xx==x2&&yy==y2)
{
ans = t.d+1;
return ;
}
book[xx][yy]=1;
q.push({xx,yy,t.d+1});
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >>n;
for(int i = 1;i<=n;i++) cin >> s[i];
cin >> x1 >> qy >> x2 >> y2;
x1--;
qy--;
x2--;
y2--;
bfs();
cout << ans;
return 0;
}