#include <bits/stdc++.h>
using namespace std;
const int MAXN = 53;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int SIGMA_MAX = 128;
pair<int, int> st[MAXN * MAXN];
pair<int, int> ed[MAXN * MAXN];
char G[MAXN][MAXN];
bool vis[MAXN][MAXN];
int to[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
using namespace std;
int bfs(bool flag, int n, int x, int y) {
  int cnt = 0;
  if (flag)
    st[cnt++] = pair<int, int>(x, y);
  else
    ed[cnt++] = pair<int, int>(x, y);
  memset(vis, false, sizeof(vis));
  queue<pair<int, int> > que;
  que.push(pair<int, int>(x, y));
  vis[x][y] = true;
  while (!que.empty()) {
    pair<int, int> last = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = last.first + to[i][0];
      int ny = last.second + to[i][1];
      if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vis[nx][ny] &&
          G[nx][ny] == '0') {
        vis[nx][ny] = true;
        if (flag)
          st[cnt++] = pair<int, int>(nx, ny);
        else
          ed[cnt++] = pair<int, int>(nx, ny);
        que.push(pair<int, int>(nx, ny));
      }
    }
  }
  return cnt;
}
signed main() {
  int n;
  scanf("%d", &n);
  int sx, sy;
  int ex, ey;
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf(" %1c", &G[i][j]);
  int cnt1 = bfs(true, n, sx, sy);
  int cnt2 = bfs(false, n, ex, ey);
  int ans = INF;
  for (int i = 0; i < cnt1; ++i) {
    for (int j = 0; j < cnt2; ++j) {
      int cost = (st[i].first - ed[j].first) * (st[i].first - ed[j].first) +
                 (st[i].second - ed[j].second) * (st[i].second - ed[j].second);
      ans = min(ans, cost);
    }
  }
  printf("%d\n", ans);
  return 0;
}
