#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0.0);
const int maxn = 1e2 + 5;
const int maxm = 4e5 + 10;
const int mod = 1e9 + 7;
int vis[maxn][maxn];
char s[maxn][maxn];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n;
void dfs(int x, int y, int cnt) {
  vis[x][y] = cnt;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 1 || nx > n || ny < 1 || ny > n || vis[nx][ny] || s[nx][ny] == '1')
      continue;
    dfs(nx, ny, cnt);
  }
}
int cal(int x1, int y1, int x2, int y2) {
  return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main() {
  cin >> n;
  int sr, sc, er, ec;
  cin >> sr >> sc >> er >> ec;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (!vis[i][j] && s[i][j] == '0') dfs(i, j, ++cnt);
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (vis[i][j] == vis[sr][sc]) {
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= n; l++) {
            if (vis[k][l] == vis[er][ec]) {
              ans = min(ans, cal(i, j, k, l));
            }
          }
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
