#include <bits/stdc++.h>
using namespace std;
int dsu[2510 + 1], siz[2510 + 1];
string str[50 + 1];
int coor(int x, int y, int n) { return (x - 1) * n + y; }
void init(int n) {
  for (int i = 1; i <= n; i++) {
    siz[i] = 1;
    dsu[i] = i;
  }
}
int root_find(int x) {
  while (x != dsu[x]) {
    dsu[x] = dsu[dsu[x]];
    x = dsu[x];
  }
  return x;
}
void uni(int x, int y) {
  x = root_find(x);
  y = root_find(y);
  if (x == y) return;
  if (siz[x] < siz[y]) {
    siz[y] += siz[x];
    dsu[x] = y;
  } else {
    siz[x] += siz[y];
    dsu[y] = x;
  }
}
int xcor(int p, int n) { return ((p - 1) / n) + 1; }
int ycor(int p, int n) { return ((p - 1) % n) + 1; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, x1, y1, x2, y2, sol = 1 << 30, a1, b1, a2, b2;
  cin >> n;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  for (int i = 0; i < n; i++) cin >> str[i];
  init(n * n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (j >= 2 && str[i - 1][j - 2] == '0' && str[i - 1][j - 1] == '0')
        uni(coor(i, j - 1, n), coor(i, j, n));
      if (i >= 2 && str[i - 2][j - 1] == '0' && str[i - 1][j - 1] == '0')
        uni(coor(i - 1, j, n), coor(i, j, n));
    }
  if (root_find(coor(x1, y1, n)) == root_find(coor(x2, y2, n))) {
    cout << 0 << "\n";
    return 0;
  }
  for (int i = 1; i < n * n; i++)
    for (int j = i + 1; j <= n * n; j++) {
      if ((root_find(coor(x1, y1, n)) == root_find(i) &&
           root_find(coor(x2, y2, n)) == root_find(j)) ||
          (root_find(coor(x1, y1, n)) == root_find(j) &&
           root_find(coor(x2, y2, n)) == root_find(i))) {
        a1 = xcor(i, n);
        b1 = ycor(i, n);
        a2 = xcor(j, n);
        b2 = ycor(j, n);
        sol = min(sol, ((a1 - a2) * (a1 - a2)) + ((b1 - b2) * (b1 - b2)));
      }
    }
  cout << sol << "\n";
  return 0;
}
