#include <bits/stdc++.h>
using namespace std;
const int M = 60;
int v[3][M][M], st1, st2, ed1, ed2, n;
char a[M][M];
void dfs(int x, int y, int p) {
  if (v[p][x][y]) return;
  v[p][x][y] = 1;
  if (x + 1 <= n && a[x + 1][y] == '0') dfs(x + 1, y, p);
  if (x - 1 >= 1 && a[x - 1][y] == '0') dfs(x - 1, y, p);
  if (y + 1 <= n && a[x][y + 1] == '0') dfs(x, y + 1, p);
  if (y - 1 >= 1 && a[x][y - 1] == '0') dfs(x, y - 1, p);
}
int main() {
  while (~scanf("%d", &n)) {
    memset(v, 0, sizeof(v));
    scanf("%d%d%d%d", &st1, &st2, &ed1, &ed2);
    for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
    dfs(st1, st2, 0);
    dfs(ed1, ed2, 1);
    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (v[0][i][j]) {
          for (int k = 1; k <= n; k++) {
            for (int l = 1; l <= n; l++) {
              if (v[1][k][l]) {
                ans = min(ans, (i - k) * (i - k) + (j - l) * (j - l));
              }
            }
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
