#include <bits/stdc++.h>
using namespace std;
int n, sx, sy, ex, ey, vis[55][55], cnt1 = 0, cnt2 = 0;
char m[55][55];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
struct node {
  int x, y;
} pt, a[10001], b[10001];
void bfs(int x, int y, int flag) {
  queue<node> q;
  node now;
  now.x = x, now.y = y;
  q.push(now);
  while (!q.empty()) {
    node s = q.front(), e;
    q.pop();
    if (vis[s.x][s.y]) continue;
    vis[s.x][s.y] = 1;
    for (int i = 0; i < 4; i++) {
      e.x = s.x + dir[i][0];
      e.y = s.y + dir[i][1];
      if (e.x <= 0 || e.x > n || e.y <= 0 || e.y > n) continue;
      if (flag == 1 && m[e.x][e.y] == '0') {
        q.push(e);
        a[cnt1].x = e.x;
        a[cnt1++].y = e.y;
      } else if (flag == 2 && m[e.x][e.y] == '0') {
        q.push(e);
        b[cnt2].x = e.x;
        b[cnt2++].y = e.y;
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  scanf("%d%d", &sx, &sy);
  scanf("%d%d", &ex, &ey);
  getchar();
  for (int i = 1; i <= n; i++) scanf("%s", m[i] + 1);
  memset(vis, 0, sizeof(vis));
  bfs(sx, sy, 1);
  memset(vis, 0, sizeof(vis));
  bfs(ex, ey, 2);
  int ans = 0x3f3f3f3f;
  a[cnt1].x = sx, a[cnt1].y = sy;
  b[cnt2].x = ex, b[cnt2].y = ey;
  for (int i = 0; i <= cnt1; i++)
    for (int j = 0; j <= cnt2; j++) {
      ans = min(ans, (a[i].x - b[j].x) * (a[i].x - b[j].x) +
                         (a[i].y - b[j].y) * (a[i].y - b[j].y));
    }
  printf("%d\n", ans);
  return 0;
}
