#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007, INF = 1e18;
long long dx[] = {0, 0, 1, -1};
long long dy[] = {-1, 1, 0, 0};
long long N;
long long r1, r2, c1, c2, cnt = 1;
string a[101];
map<pair<long long, long long>, long long> mp;
void dfs(long long i, long long j) {
  mp[pair<long long, long long>(i, j)] = cnt;
  for (long long k = 0; k < 4; k++) {
    long long ni = i + dx[k];
    long long nj = j + dy[k];
    if (ni < 0 || N <= ni || nj < 0 || N <= nj) continue;
    if (a[ni][nj] == '1') continue;
    if (mp[pair<long long, long long>(ni, nj)]) continue;
    dfs(ni, nj);
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;
  r2--;
  c2--;
  for (long long i = 0; i < N; i++) cin >> a[i];
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      if (a[i][j] == '0' && mp[pair<long long, long long>(i, j)] == 0) {
        dfs(i, j);
        cnt++;
      }
    }
  }
  long long ans = INF, st = mp[pair<long long, long long>(r1, c1)],
            gl = mp[pair<long long, long long>(r2, c2)];
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      if (mp[pair<long long, long long>(i, j)] != st) continue;
      for (long long k = 0; k < N; k++) {
        for (long long l = 0; l < N; l++) {
          if (mp[pair<long long, long long>(k, l)] != gl) continue;
          ans = min((ans), ((i - k) * (i - k) + (j - l) * (j - l)));
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
