#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long n, long long lelo) {
  if (n == 0) return 1;
  if (n == 1) return a;
  if (n == 2) return (a * a) % lelo;
  if (n % 2)
    return (a * power(a, n - 1, lelo)) % lelo;
  else
    return power(power(a, n / 2, lelo), 2, lelo) % lelo;
}
void swap(long long &a, long long &b) {
  long long tm = a;
  a = b;
  b = tm;
}
const long long N = 1e+5;
long long a[50][50];
string s[50];
long long n, r2, r1, c1, c2;
vector<pair<long long, long long> > v;
void dfs(long long x, long long y) {
  v.push_back({x, y});
  a[x][y] = 1;
  if (x + 1 < n)
    if (a[x + 1][y] == 0 && s[x + 1][y] == '0') dfs(x + 1, y);
  if (x - 1 >= 0)
    if (a[x - 1][y] == 0 && s[x - 1][y] == '0') dfs(x - 1, y);
  if (y + 1 < n)
    if (a[x][y + 1] == 0 && s[x][y + 1] == '0') dfs(x, y + 1);
  if (y - 1 >= 0)
    if (a[x][y - 1] == 0 && s[x][y - 1] == '0') dfs(x, y - 1);
}
void solve() {
  for (long long(i) = (0); (i) < (50); (i)++)
    for (long long(j) = (0); (j) < (50); (j)++) a[i][j] = 0;
  cin >> n >> r1 >> c1 >> r2 >> c2;
  for (long long(i) = (0); (i) < (n); (i)++) cin >> s[i];
  vector<pair<long long, long long> > v1, v2;
  v.clear();
  dfs(r1 - 1, c1 - 1);
  v1 = v;
  v.clear();
  dfs(r2 - 1, c2 - 1);
  v2 = v;
  long long ct = 0, ans = 1e4;
  for (auto &(i) : (v1))
    for (auto &(j) : (v2)) {
      ct = (i.first - j.first) * (i.first - j.first) +
           (i.second - j.second) * (i.second - j.second);
      ans = min(ct, ans);
    }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long double tt = 1.0 * clock();
  long long t = 1;
  for (long long(i) = (1); (i) < (t + 1); (i)++) {
    solve();
    if (i < t) cout << "\n";
  }
  tt = 1.000000000 * (clock() - tt) / CLOCKS_PER_SEC;
  return 0;
}
