#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9, MOD = INF + 7;
long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); }
long long power(long long a, long long b) {
  return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1));
}
const long long N = 5e1 + 20, M = 2e1 + 2;
long long n;
char a[N][N];
bool marked[N][N];
long long dx[] = {+1, -1, 0, 0};
long long dy[] = {0, 0, +1, -1};
bool isVal(long long x, long long y) {
  return (x >= 1 && y >= 1 && x <= n && y <= n && a[x][y] == '0');
}
void dfs(long long x, long long y, vector<pair<long long, long long> > &v) {
  marked[x][y] = true;
  for (long long i = 0; i < 4; i++) {
    long long xx = x + dx[i], yy = y + dy[i];
    if (isVal(xx, yy) && !marked[xx][yy]) dfs(xx, yy, v);
  }
  v.push_back(make_pair(x, y));
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  pair<long long, long long> src;
  cin >> src.first >> src.second;
  pair<long long, long long> dest;
  cin >> dest.first >> dest.second;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) cin >> a[i][j];
  vector<pair<long long, long long> > first, second;
  dfs(src.first, src.second, first);
  if (marked[dest.first][dest.second]) return cout << 0 << "\n", 0;
  memset(marked, 0, sizeof marked);
  dfs(dest.first, dest.second, second);
  long long mn = INF;
  for (pair<long long, long long> x : first)
    for (pair<long long, long long> y : second)
      mn = min(mn, (x.first - y.first) * (x.first - y.first) +
                       (x.second - y.second) * (x.second - y.second));
  cout << mn << "\n";
  return 0;
}
