#include <bits/stdc++.h>
using namespace std;
int vis[51][51];
vector<pair<int, int> > v1;
vector<pair<int, int> > v2;
char a[51][51];
int nx[4] = {0, 1, 0, -1};
int ny[4] = {1, 0, -1, 0};
int n;
void dfs(int x, int y, int z) {
  vis[x][y] = z;
  if (z == 1)
    v1.push_back({x, y});
  else if (z == 2)
    v2.push_back({x, y});
  for (int i = 0; i < 4; i++) {
    int xx = x + nx[i];
    int yy = y + ny[i];
    if (xx >= 0 && xx < n && yy >= 0 && yy < n && a[xx][yy] == '0' &&
        vis[xx][yy] == 0)
      dfs(xx, yy, z);
  }
}
int main() {
  int sx, sy, ex, ey;
  while (~scanf("%d", &n)) {
    scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
    sx--;
    sy--;
    ex--;
    ey--;
    for (int i = 0; i < n; i++) scanf("%s", a[i]);
    memset(vis, 0, sizeof(vis));
    dfs(sx, sy, 1);
    if (vis[ex][ey] == 1) {
      puts("0");
      continue;
    }
    dfs(ex, ey, 2);
    long long ans = 1e9;
    for (int i = 0; i < v1.size(); i++)
      for (int j = 0; j < v2.size(); j++) {
        long long sum =
            (v1[i].first - v2[j].first) * (v1[i].first - v2[j].first) +
            (v1[i].second - v2[j].second) * (v1[i].second - v2[j].second);
        ans = min(ans, sum);
      }
    printf("%I64d\n", ans);
  }
  return 0;
}
