#include <bits/stdc++.h>
using namespace std;
const int N = 100;
pair<int, int >st, ed;
int n;
int vis[N][N] = { 0 };
char a[N][N];
struct node {
int x, y, cnt;
};
queue<node>q;
int dy[] = { -1,0,1,0 };
int dx[] = { 0,1,0,-1 };
int bfs(int x, int y, int cnt) {
q.push(node{ x,y,cnt });
vis[x][y] = 1;
while (!q.empty()) {
node u = q.front();
if (ed.first == u.x && ed.second == u.y) return u.cnt;
q.pop();
int x = u.x, y = u.y;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < 0 || tx > n || ty < 0 || ty > n) continue;
if (vis[tx][ty] || a[tx][ty] == '1') continue;
vis[tx][ty] = 1;
q.push(node{ tx ,ty ,cnt + 1 });
}
}
return -1;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%s", a[i]+1);
}
cin >> st.first >> st.second >> ed.first >> ed.second;
cout << bfs(st.first, st.second, 0) << endl;
return 0;
}