#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 10;
char a[100][100];
long long n, r1, r2, c1, c2;
vector<pair<long long, long long> > vec[10];
bool mark[100][100];
long long C[100][100];
long long c, ans;
void DFS(long long x, long long y) {
  mark[x][y] = true;
  C[x][y] = c;
  vec[c].push_back({x, y});
  if (mark[x][y - 1] != true) {
    DFS(x, y - 1);
  }
  if (mark[x][y + 1] != true) {
    DFS(x, y + 1);
  }
  if (mark[x - 1][y] != true) {
    DFS(x - 1, y);
  }
  if (mark[x + 1][y] != true) {
    DFS(x + 1, y);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  cin >> r1 >> c1 >> r2 >> c2;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> a[i][j];
      if (a[i][j] == '1') {
        mark[i][j] = true;
      }
    }
  }
  for (long long i = 0; i <= n + 1; i++) {
    mark[0][i] = mark[n + 1][i] = mark[i][0] = mark[i][n + 1] = true;
  }
  c++;
  DFS(r1, c1);
  c++;
  DFS(r2, c2);
  if (C[r1][c1] == C[r2][c2]) {
    cout << 0;
    return 0;
  }
  ans = 1e18;
  for (auto X : vec[1]) {
    for (auto Y : vec[2]) {
      ans = min(ans, abs(X.first - Y.first) * abs(X.first - Y.first) +
                         abs(X.second - Y.second) * abs(X.second - Y.second));
    }
  }
  cout << ans;
  return 0;
}
