#include <bits/stdc++.h>
using namespace std;
char a[55][55];
int walk[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct node {
  int x, y;
};
queue<node> q, q1;
int vis[55][55], vis1[55][55], n;
void bfs(int s, int t, int judge) {
  node qi;
  qi.x = s, qi.y = t;
  q.push(qi);
  if (judge)
    vis[s][t] = 1;
  else
    vis1[s][t] = 1;
  while (!q.empty()) {
    node p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = p.x + walk[i][0], yy = p.y + walk[i][1];
      if (xx <= 0 || xx > n || yy <= 0 || yy > n || a[xx][yy] == '1') continue;
      if (judge && vis[xx][yy])
        continue;
      else if (!judge && vis1[xx][yy])
        continue;
      if (judge)
        vis[xx][yy] = 1;
      else
        vis1[xx][yy] = 1;
      node o;
      o.x = xx, o.y = yy;
      q.push(o);
    }
  }
}
int main() {
  cin >> n;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
  }
  bfs(sx, sy, 1);
  bfs(tx, ty, 0);
  int minn = 1e8;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int i1 = 1; i1 <= n; i1++) {
        for (int j1 = 1; j1 <= n; j1++) {
          if (vis[i][j] && vis1[i1][j1]) {
            minn = min(minn, (i - i1) * (i - i1) + (j - j1) * (j - j1));
          }
        }
      }
    }
  }
  printf("%d\n", minn);
  return 0;
}
