#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool f = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = 1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return f ? -x : x;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(long long x) {
  write(x);
  puts("");
}
int co[55][55], cnt, n, r1, r2, c1, c2, ans = 1 << 30;
char s[55][55];
queue<pair<int, int> > q;
const int mx[4] = {-1, 0, 1, 0}, my[4] = {0, 1, 0, -1};
void bfs(int x, int y) {
  co[x][y] = cnt;
  q.push(pair<int, int>(x, y));
  while (q.size()) {
    int X = q.front().first, Y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int u = X + mx[i], v = Y + my[i];
      if (u < 1 || u > n || v < 1 || v > n || co[u][v] || s[u][v] == '1')
        continue;
      co[u][v] = cnt;
      q.push(pair<int, int>(u, v));
    }
  }
}
int Dis(int a, int b, int c, int d) {
  return ((a - c) * (a - c)) + ((b - d) * (b - d));
}
signed main() {
  n = read();
  r1 = read();
  c1 = read();
  r2 = read();
  c2 = read();
  ans = Dis(r1, c1, r2, c2);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      if (!co[i][j] && s[i][j] != '1') cnt++, bfs(i, j);
  }
  if (co[r1][c1] == co[r2][c2]) return puts("0"), 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int t = 1; t <= n; t++) {
        for (int k = 1; k <= n; k++)
          if (co[i][j] == co[r1][c1] && co[t][k] == co[r2][c2])
            ans = min(ans, Dis(i, j, t, k));
      }
    }
  }
  write(ans);
  return 0;
}
