#include <bits/stdc++.h>
using namespace std;
const long long N = 1000043;
long long mod = 1000000007;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    b /= 2;
    a = (a * a) % mod;
  }
  return ans;
}
long long bit(long long n) {
  if (n == 0) return 0;
  long long k = 1, b = 0;
  while ((n / k) != 0) {
    k *= 2;
    b++;
  }
  return b - 1;
}
long long n, t, k, m, x, y, flag = 1;
string s;
long long mat[55][55];
vector<pair<long long, long long> > reach;
void dfs(long long x, long long y) {
  if (x >= n || y >= n || x < 0 || y < 0) return;
  if (mat[x][y] == 1) return;
  mat[x][y] = 1;
  reach.push_back(make_pair(x, y));
  dfs(x + 1, y);
  dfs(x - 1, y);
  dfs(x, y + 1);
  dfs(x, y - 1);
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  long long x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, x2--, y1--, y2--;
  string s[55];
  for (long long i = 0; i < n; i++) cin >> s[i];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      (mat[i][j] = s[i][j] - '0');
    }
  }
  reach.clear();
  dfs(x1, y1);
  vector<pair<long long, long long> > src = reach;
  reach.clear();
  dfs(x2, y2);
  vector<pair<long long, long long> > dest = reach;
  long long curr = 0, ans = INT_MAX;
  for (long long i = 0; i < src.size(); i++) {
    for (long long j = 0; j < dest.size(); j++) {
      curr = (pow((src[i].first - dest[j].first), 2)) +
             (pow((src[i].second - dest[j].second), 2));
      ans = min(ans, curr);
    }
  }
  if (src.size() == 0 || dest.size() == 0) ans = 0;
  cout << ans;
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
  return 0;
}
