#include <bits/stdc++.h>
using namespace std;
const int N = 100;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int dis[N][N][2], vis[N][N][2];
int n, x[2], y[2];
struct node {
  int x, y, z, d;
  bool operator<(const node &a) const { return d > a.d; }
} now;
priority_queue<node> q;
char mp[N][N];
int sqr(int x) { return x * x; }
int main() {
  cin >> n;
  cin >> x[0] >> y[0] >> x[1] >> y[1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) dis[i][j][0] = dis[i][j][1] = 1e9;
  for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  dis[x[0]][y[0]][0] = 0;
  q.push((node){x[0], y[0], 0, 0});
  while (!q.empty()) {
    now = q.top();
    q.pop();
    if (vis[now.x][now.y][now.z]) continue;
    vis[now.x][now.y][now.z] = 1;
    for (int nx, ny, i = 0; i < 4; i++) {
      nx = now.x + dx[i], ny = now.y + dy[i];
      if (nx <= 0 || nx > n || ny <= 0 || ny > n || mp[nx][ny] == '1') continue;
      if (now.d < dis[nx][ny][now.z])
        q.push((node){nx, ny, now.z, dis[nx][ny][now.z] = now.d});
    }
    if (now.z == 0) {
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
          if (mp[i][j] == '0' && !vis[i][j][1] &&
              now.d + sqr(now.x - i) + sqr(now.y - j) < dis[i][j][now.z])
            q.push((node){i, j, 1,
                          dis[i][j][now.z] =
                              min(dis[i][j][now.z],
                                  now.d + sqr(now.x - i) + sqr(now.y - j))});
    }
  }
  cout << min(dis[x[1]][y[1]][0], dis[x[1]][y[1]][1]);
  return 0;
}
