#include <bits/stdc++.h>
using namespace std;
using namespace std;
vector<pair<long long, long long> > v;
vector<pair<long long, long long> > vei;
vector<long long> h;
char a[52][52];
long long vi[52][52] = {0};
long long s = 0, d = 0;
long long min(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}
void dfs(long long r, long long co, long long c) {
  vi[r][co] = 1;
  if (c == 0) {
    v.push_back(make_pair(r, co));
    s++;
  } else {
    vei.push_back(make_pair(r, co));
    d++;
  }
  if (a[r + 1][co] == '0' && vi[r + 1][co] == 0) {
    if (c == 0)
      dfs(r + 1, co, 0);
    else
      dfs(r + 1, co, 1);
  }
  if (a[r - 1][co] == '0' && vi[r - 1][co] == 0) {
    if (c == 0)
      dfs(r - 1, co, 0);
    else
      dfs(r - 1, co, 1);
  }
  if (a[r][co + 1] == '0' && vi[r][co + 1] == 0) {
    if (c == 0)
      dfs(r, co + 1, 0);
    else
      dfs(r, co + 1, 1);
  }
  if (a[r][co - 1] == '0' && vi[r][co - 1] == 0) {
    if (c == 0)
      dfs(r, co - 1, 0);
    else
      dfs(r, co - 1, 1);
  }
}
void solve() {
  long long n;
  cin >> n;
  long long r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  for (long long i = 1; i < n + 1; i++) {
    for (long long j = 1; j < n + 1; j++) {
      cin >> a[i][j];
    }
  }
  dfs(r1, c1, 0);
  dfs(r2, c2, 1);
  long long ma = (long long)1e18;
  for (long long i = 0; i < s; i++) {
    for (long long j = 0; j < d; j++) {
      ma = min(ma, pow((v[i].first - vei[j].first), 2) +
                       pow((v[i].second - vei[j].second), 2));
    }
  }
  cout << ma << "\n";
}
int main() {
  ios::sync_with_stdio(true);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
