#include <bits/stdc++.h>
using namespace std;
string second[1000];
long long dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
long long n;
vector<pair<long long, long long> > v, v2;
long long mat[1001][1001];
void dfs(long long s1, long long e) {
  if (mat[s1][e] == 1) return;
  mat[s1][e] = 1;
  for (int i = 0; i < 4; i++) {
    long long x = s1 + dx[i], y = e + dy[i];
    if (x >= 0 && y >= 0 && x < n && y < n && mat[x][y] == 0 &&
        second[x][y] == '1') {
      v.push_back({s1 + 1, e + 1});
      mat[x][y] = 1;
      continue;
    }
    if (mat[x][y] == 0 && x >= 0 && y >= 0 && x < n && y < n) dfs(x, y);
  }
}
void dfs1(long long s1, long long e) {
  if (mat[s1][e] == 1) return;
  mat[s1][e] = 1;
  for (int i = 0; i < 4; i++) {
    long long x = s1 + dx[i], y = e + dy[i];
    if (x >= 0 && y >= 0 && x < n && y < n && mat[x][y] == 0 &&
        second[x][y] == '1') {
      v2.push_back({s1 + 1, e + 1});
      mat[x][y] = 1;
      continue;
    }
    if (mat[x][y] == 0 && x >= 0 && y >= 0 && x < n && y < n) dfs1(x, y);
  }
}
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  cin >> n;
  long long x, y, a, b;
  cin >> a >> b >> x >> y;
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < n + 1; j++) mat[i][j] = 0;
  for (int i = 0; i < n; i++) cin >> second[i];
  dfs(a - 1, b - 1);
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < n + 1; j++) mat[i][j] = 0;
  dfs1(x - 1, y - 1);
  long long ans = INT_MAX;
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v2.size(); j++) {
      long long chk = abs(v[i].first - v2[j].first);
      chk *= chk;
      long long chk1 = abs(v[i].second - v2[j].second);
      chk1 *= chk1;
      ans = min(ans, chk + chk1);
    }
  }
  if (v.size() == 0)
    cout << 0 << endl;
  else
    cout << ans << endl;
}
