#include <bits/stdc++.h>
using namespace std;
vector<int> g[55 * 55];
int comp[55 * 55], compAtu;
int n;
int conv(int r, int c) { return (r - 1) * n + c; }
long long dist(int v1, int v2) {
  int c1 = v1 % n;
  if (c1 == 0) c1 = n;
  v1 -= c1;
  int r1 = v1 / n + 1;
  int c2 = v2 % n;
  if (c2 == 0) c2 = n;
  v2 -= c2;
  int r2 = v2 / n + 1;
  return (r2 - r1) * (r2 - r1) + (c2 - c1) * (c2 - c1);
}
void dfs(int u) {
  comp[u] = compAtu;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (comp[v] == 0) {
      dfs(v);
    }
  }
}
int main() {
  scanf("%d", &n);
  int r1, c1;
  scanf("%d %d", &r1, &c1);
  int r2, c2;
  scanf("%d %d", &r2, &c2);
  int mat[55][55];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char ch;
      scanf(" %c ", &ch);
      mat[i][j] = ch - '0';
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (mat[i][j] == 1) continue;
      if (i != 1 && mat[i - 1][j] == 0) {
        g[conv(i, j)].push_back(conv(i - 1, j));
      }
      if (i != n && mat[i + 1][j] == 0) {
        g[conv(i, j)].push_back(conv(i + 1, j));
      }
      if (j != 1 && mat[i][j - 1] == 0) {
        g[conv(i, j)].push_back(conv(i, j - 1));
      }
      if (j != n && mat[i][j + 1] == 0) {
        g[conv(i, j)].push_back(conv(i, j + 1));
      }
    }
  }
  memset(comp, 0, sizeof comp);
  compAtu = 0;
  for (int i = 1; i <= n * n; i++) {
    int auxc = i % n;
    if (auxc == 0) auxc = n;
    int auxr = (i - auxc) / n + 1;
    if (mat[auxr][auxc] == 1) continue;
    if (comp[i] == 0) {
      compAtu++;
      dfs(i);
    }
  }
  int v1 = conv(r1, c1), v2 = conv(r2, c2);
  if (comp[v1] == comp[v2]) {
    printf("0");
    return 0;
  }
  long long ans = 0x3f3f3f3f;
  for (int i = 1; i <= n * n; i++) {
    if (comp[i] != comp[v2] && comp[i] != comp[v1]) continue;
    for (int j = 1; j <= n * n; j++) {
      if (comp[j] == comp[i]) continue;
      if (comp[j] != comp[v2] && comp[j] != comp[v1]) continue;
      ans = min(ans, dist(i, j));
    }
  }
  printf("%lld", ans);
  return 0;
}
