#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (b > 0) {
    long long tmp = a;
    a = b;
    b = tmp % b;
  }
  return a;
}
const int M = 1e9 + 7;
long long powmod(long long a, long long b, long long mod) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}
const int N = 1e5 + 10;
int n, x1, y11, x2, y2;
string s[100];
vector<pair<int, int> > v1, v2;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int mark1[100][100], mark2[100][100];
void dfs1(int r, int c) {
  for (int i = 0; i < 4; ++i) {
    int rr = r + dx[i];
    int cc = c + dy[i];
    if ((rr >= 0 && rr < n && cc >= 0 && cc < n) && (mark1[rr][cc] == 0) &&
        (s[rr][cc] == '0')) {
      mark1[rr][cc] = 1;
      v1.push_back(make_pair(rr, cc));
      dfs1(rr, cc);
    }
  }
}
void dfs2(int r, int c) {
  for (int i = 0; i < 4; ++i) {
    int rr = r + dx[i];
    int cc = c + dy[i];
    if ((rr >= 0 && rr < n && cc >= 0 && cc < n) && (mark2[rr][cc] == 0) &&
        (s[rr][cc] == '0')) {
      mark2[rr][cc] = 1;
      v2.push_back(make_pair(rr, cc));
      dfs2(rr, cc);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x1 >> y11 >> x2 >> y2;
  x1--, y11--, x2--, y2--;
  for (int i = 0; i < n; ++i) cin >> s[i];
  v1.push_back(make_pair(x1, y11));
  mark1[x1][y11] = 1;
  dfs1(x1, y11);
  v2.push_back(make_pair(x2, y2));
  mark2[x2][y2] = 1;
  dfs2(x2, y2);
  int ans = M;
  for (int i = 0; i < v1.size(); ++i) {
    for (int j = 0; j < v2.size(); ++j) {
      int fir = (v1[i].first - v2[j].first) * (v1[i].first - v2[j].first);
      int sec = (v1[i].second - v2[j].second) * (v1[i].second - v2[j].second);
      ans = min(ans, fir + sec);
    }
  }
  cout << ans << endl;
  return 0;
}
