#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
long long xi, yi, xf, yf, n;
vector<pair<long long int, long long int> > v[2505];
bool vis[2505 + 2][2505 + 2];
bool vis2[2505 + 2][2505 + 2];
vector<pair<long long int, long long int> > a, b;
void dfs(long long x, long long y) {
  a.push_back(make_pair(x, y));
  vis[x][y] = 1;
  if (x > 0 && v[x - 1][y].second == 0 && !vis[x - 1][y]) dfs(x - 1, y);
  if (x < n - 1 && v[x + 1][y].second == 0 && !vis[x + 1][y]) dfs(x + 1, y);
  if (y > 0 && v[x][y - 1].second == 0 && !vis[x][y - 1]) dfs(x, y - 1);
  if (y < n - 1 && v[x][y + 1].second == 0 && !vis[x][y + 1]) dfs(x, y + 1);
}
void dfs2(long long x, long long y) {
  b.push_back(make_pair(x, y));
  vis2[x][y] = 1;
  if (x > 0 && v[x - 1][y].second == 0 && !vis2[x - 1][y]) dfs2(x - 1, y);
  if (x < n - 1 && v[x + 1][y].second == 0 && !vis2[x + 1][y]) dfs2(x + 1, y);
  if (y > 0 && v[x][y - 1].second == 0 && !vis2[x][y - 1]) dfs2(x, y - 1);
  if (y < n - 1 && v[x][y + 1].second == 0 && !vis2[x][y + 1]) dfs2(x, y + 1);
}
void solve() {
  long long i, j;
  char x;
  cin >> n;
  cin >> xi >> yi >> xf >> yf;
  xi--;
  xf--;
  yi--;
  yf--;
  for (i = 0; i <= n - 1; i++) {
    for (j = 0; j <= n - 1; j++) {
      cin >> x;
      v[i].push_back(make_pair(j, x - '0'));
    }
  }
  dfs(xi, yi);
  dfs2(xf, yf);
  long long ans = (1ll << 60);
  for (i = 0; i <= (long long int)a.size() - 1; i++) {
    for (j = 0; j <= (long long int)b.size() - 1; j++) {
      ans = min((a[i].first - b[j].first) * (a[i].first - b[j].first) +
                    (a[i].second - b[j].second) * (a[i].second - b[j].second),
                ans);
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    solve();
  }
  return 0;
}
