#include <bits/stdc++.h>
using namespace std;
void nextPlease() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
bool vissrc[50][50];
bool visdst[50][50];
char grid[50][50];
int n;
bool exist(int i, int j) {
  if ((i >= n) || (i < 0)) {
    return false;
  }
  if ((j >= n) || j < 0) {
    return false;
  }
  if (grid[i][j] == '1') {
    return false;
  }
  return true;
}
void checkpath(int x, int y, bool vis[50][50]) {
  if ((exist(x + 1, y)) && (!vis[x + 1][y])) {
    vis[x + 1][y] = true;
    checkpath(x + 1, y, vis);
  }
  if ((exist(x - 1, y)) && (!vis[x - 1][y])) {
    vis[x - 1][y] = true;
    checkpath(x - 1, y, vis);
  }
  if ((exist(x, y + 1)) && (!vis[x][y + 1])) {
    vis[x][y + 1] = true;
    checkpath(x, y + 1, vis);
  }
  if (exist(x, y - 1) && !vis[x][y - 1]) {
    vis[x][y - 1] = true;
    checkpath(x, y - 1, vis);
  }
}
int main() {
  long long ans = LONG_LONG_MAX;
  nextPlease();
  cin >> n;
  pair<int, int> source;
  pair<int, int> dist;
  cin >> source.first >> source.second >> dist.first >> dist.second;
  for (int i = 0; i < n; ++i) {
    cin >> grid[i];
  }
  source.first--;
  source.second--;
  dist.first--;
  dist.second--;
  vissrc[source.first][source.second] = true;
  visdst[dist.first][dist.second] = true;
  checkpath(source.first, source.second, vissrc);
  checkpath(dist.first, dist.second, visdst);
  for (int x1 = 0; x1 < n; x1++) {
    for (int y1 = 0; y1 < n; y1++) {
      for (int x2 = 0; x2 < n; x2++) {
        for (int y2 = 0; y2 < n; y2++) {
          if (visdst[x1][y1] && vissrc[x2][y2]) {
            long long cost =
                pow(((x1 + 1) - (x2 + 1)), 2) + pow(((y1 + 1) - (y2 + 1)), 2);
            ans = min(ans, cost);
          }
        }
      }
    }
  }
  cout << ans;
}
