#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int sx, sy, tx, ty, n;
int vis[maxn][maxn], bel[maxn][maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char mp[maxn][maxn];
struct node {
  int x, y;
} cur, nxt;
void bfs(int x, int y, int col) {
  queue<node> q;
  cur.x = x;
  cur.y = y;
  q.push(cur);
  vis[x][y] = 1;
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    int x = cur.x, y = cur.y;
    bel[x][y] = col;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx < 0 || xx >= n || yy < 0 || yy >= n || vis[xx][yy] ||
          mp[xx][yy] == '1')
        continue;
      nxt.x = xx;
      nxt.y = yy;
      vis[xx][yy] = 1;
      q.push(nxt);
    }
  }
}
int dis(int x1, int y1, int x2, int y2) {
  return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main() {
  scanf("%d", &n);
  scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
  sx--;
  sy--;
  tx--;
  ty--;
  for (int i = 0; i < n; i++) scanf("%s", mp[i]);
  int cnt = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      if (mp[i][j] == '0' && !vis[i][j]) bfs(i, j, cnt++);
  }
  int p1 = bel[sx][sy], p2 = bel[tx][ty];
  int ans = 0x3f3f3f3f;
  if (p1 == p2) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (mp[i][j] == '1' || bel[i][j] != p1) continue;
      for (int k = 0; k < n; k++)
        for (int l = 0; l < n; l++) {
          if (mp[k][l] == '1' || bel[k][l] != p2) continue;
          ans = min(ans, dis(i, j, k, l));
        }
    }
  printf("%d\n", ans);
}
