#include <bits/stdc++.h>
using namespace std;
long long n;
bool ch[51][51];
long long d[51][51];
vector<pair<long long, long long>> v;
long long l1, r1, l2, r2;
char c[51][51];
long long ans = 1e18;
void check(long long l, long long r) {
  if (ch[l][r]) return;
  if (l == l1 and r == r1) {
    cout << 0;
    exit(0);
  }
  v.push_back({l, r});
  d[l][r] = abs(l2 - l) + abs(r2 - r);
  ch[l][r] = true;
  if (c[l + 1][r] == '0') check(l + 1, r);
  if (c[l][r + 1] == '0') check(l, r + 1);
  if (c[l - 1][r] == '0') check(l - 1, r);
  if (c[l][r - 1] == '0') check(l, r - 1);
}
long long sqr(long long x) {
  x = abs(x);
  return x * x;
}
void uradi(long long l, long long r) {
  if (ch[l][r]) return;
  ch[l][r] = true;
  for (auto i : v) {
    ans = min(ans, sqr(i.first - l) + sqr(i.second - r));
  }
  if (c[l + 1][r] == '0') uradi(l + 1, r);
  if (c[l][r + 1] == '0') uradi(l, r + 1);
  if (c[l - 1][r] == '0') uradi(l - 1, r);
  if (c[l][r - 1] == '0') uradi(l, r - 1);
}
int main() {
  cin >> n;
  cin >> l1 >> r1 >> l2 >> r2;
  if (l1 == l2 and r1 == r2) return cout << 0, 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> c[i][j];
    }
  }
  check(l2, r2);
  memset(ch, false, sizeof(ch));
  uradi(l1, r1);
  cout << ans;
  return 0;
}
