#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 50;
char a[maxn][maxn];
struct node {
  int x, y;
  node() {}
  node(int xx, int yy) {
    x = xx;
    y = yy;
  }
} q1[maxn], q2[maxn];
int vis[maxn][maxn];
int f[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
int n;
bool judge(int t1, int t2) {
  if (t1 >= 1 && t1 <= n && t2 >= 1 && t2 <= n) return true;
  return false;
}
int num1 = 0, num2 = 0;
bool bfs1(int x1, int y1, int x2, int y2) {
  queue<node> q;
  q.push(node(x1, y1));
  vis[x1][y1] = 1;
  q1[++num1].x = x1;
  q1[num1].y = y1;
  while (!q.empty()) {
    node top = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = top.x + f[0][i];
      int y = top.y + f[1][i];
      if (judge(x, y) && vis[x][y] == 0 && a[x][y] == '0') {
        vis[x][y] = 1;
        q.push(node(x, y));
        q1[++num1].x = x;
        q1[num1].y = y;
      }
    }
  }
  return vis[x2][y2] == 1;
}
void bfs2(int x, int y) {
  queue<node> q;
  q.push(node(x, y));
  vis[x][y] = 1;
  q2[++num2].x = x;
  q2[num2].y = y;
  while (!q.empty()) {
    node top = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = top.x + f[0][i];
      int y = top.y + f[1][i];
      if (judge(x, y) && vis[x][y] == 0 && a[x][y] == '0') {
        vis[x][y] = 1;
        q.push(node(x, y));
        q2[++num2].x = x;
        q2[num2].y = y;
      }
    }
  }
}
int cal(int x1, int y1, int x2, int y2) {
  return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main() {
  cin >> n;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] + 1;
  }
  memset(vis, 0, sizeof(vis));
  bfs1(x1, y1, x2, y2);
  memset(vis, 0, sizeof(vis));
  bfs2(x2, y2);
  int minn = 0x3f3f3f3f;
  for (int i = 1; i <= num1; i++) {
    for (int j = 1; j <= num2; j++) {
      minn = min(minn, cal(q1[i].x, q1[i].y, q2[j].x, q2[j].y));
    }
  }
  printf("%d\n", minn);
  return 0;
}
