#include <bits/stdc++.h>
using namespace std;
void Weapons19() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
vector<pair<long long, long long>> a, b;
void dfs(vector<vector<long long>> &vis, vector<string> &v, long long n,
         long long r, long long c) {
  if (vis[r][c] == 1) return;
  if (v[r][c] == '1') return;
  vis[r][c] = 1;
  a.push_back(make_pair(r, c));
  if (r > 0 && vis[r - 1][c] == 0) {
    dfs(vis, v, n, r - 1, c);
  }
  if (c > 0 && vis[r][c - 1] == 0) {
    dfs(vis, v, n, r, c - 1);
  }
  if (r + 1 < n && vis[r + 1][c] == 0) dfs(vis, v, n, r + 1, c);
  if (c + 1 < n && vis[r][c + 1] == 0) dfs(vis, v, n, r, c + 1);
}
void dfs2(vector<vector<long long>> &vis, vector<string> &v, long long n,
          long long r, long long c) {
  if (vis[r][c] == 1) return;
  if (v[r][c] == '1') return;
  vis[r][c] = 1;
  b.push_back(make_pair(r, c));
  if (r > 0) {
    dfs2(vis, v, n, r - 1, c);
  }
  if (c > 0) {
    dfs2(vis, v, n, r, c - 1);
  }
  if (r < n - 1) dfs2(vis, v, n, r + 1, c);
  if (c < n - 1) dfs2(vis, v, n, r, c + 1);
}
int32_t main() {
  Weapons19();
  long long n;
  cin >> n;
  long long r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;
  r2--;
  c2--;
  vector<string> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  vector<vector<long long>> vis(n, vector<long long>(n, 0));
  dfs(vis, v, n, r1, c1);
  vector<vector<long long>> vis2(n, vector<long long>(n, 0));
  dfs2(vis2, v, n, r2, c2);
  long long ans = LLONG_MAX;
  for (long long i = 0; i < a.size(); i++) {
    for (long long j = 0; j < b.size(); j++) {
      long long p = (a[i].second - b[j].second) * (a[i].second - b[j].second) +
                    (a[i].first - b[j].first) * (a[i].first - b[j].first);
      ans = min(ans, p);
    }
  }
  cout << ans;
}
