#include <bits/stdc++.h>
using namespace std;
int n;
bool vis[100][100];
char g[100][100];
void dfs(int a, int b) {
  if (vis[a][b] || a > n || a < 1 || b < 1 || b > n || g[a][b] == '1') return;
  vis[a][b] = 1;
  dfs(a + 1, b);
  dfs(a, b + 1);
  dfs(a - 1, b);
  dfs(a, b - 1);
}
int dist(pair<int, int> a, pair<int, int> b) {
  return (a.first - b.first) * (a.first - b.first) +
         (a.second - b.second) * (a.second - b.second);
}
int main() {
  cin >> n;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> g[i][j];
    }
  }
  set<pair<int, int>> a;
  set<pair<int, int>> b;
  dfs(r1, c1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (vis[i][j]) a.insert({i, j});
      vis[i][j] = 0;
    }
  }
  dfs(r2, c2);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (vis[i][j]) b.insert({i, j});
    }
  }
  if (a == b)
    cout << 0 << endl;
  else {
    int s = 10000000;
    for (auto x : a) {
      for (auto y : b) {
        s = min(s, dist(x, y));
      }
    }
    cout << s << endl;
  }
  return 0;
}
