#include <bits/stdc++.h>
using namespace std;
int X[] = {-1, 0, 1, 0};
int Y[] = {0, -1, 0, 1};
vector<pair<int, int> > A, B;
int mapa[60][60], M[60][60];
queue<pair<int, int> > q;
int f(pair<int, int> a, pair<int, int> b) {
  int x = b.first - a.first, y = b.second - a.second;
  return x * x + y * y;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, a, b, a1, b1, m = 1e9 + 7, x, y;
  char c;
  cin >> n;
  cin >> a >> b >> a1 >> b1;
  a--, b--, b1--, a1--;
  for (x = 0; x < n; x++) {
    for (y = 0; y < n; y++) {
      cin >> c;
      mapa[x][y] = c - '0';
    }
  }
  q.push(make_pair(a, b));
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (M[x][y]) continue;
    M[x][y] = 1;
    A.push_back(make_pair(x, y));
    for (int i = 0; i < 4; i++) {
      int x1 = x + X[i];
      int y1 = y + Y[i];
      if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && M[x1][y1] == 0 &&
          mapa[x1][y1] == 0)
        q.push(make_pair(x1, y1));
    }
  }
  if (M[a1][b1]) {
    cout << 0 << endl;
    return 0;
  }
  q.push(make_pair(a1, b1));
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (M[x][y]) continue;
    M[x][y] = 1;
    B.push_back(make_pair(x, y));
    for (int i = 0; i < 4; i++) {
      int x1 = x + X[i];
      int y1 = y + Y[i];
      if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && M[x1][y1] == 0 &&
          mapa[x1][y1] == 0)
        q.push(make_pair(x1, y1));
    }
  }
  for (x = 0; x < A.size(); x++) {
    pair<int, int> p = A[x];
    for (y = 0; y < B.size(); y++) {
      pair<int, int> k = B[y];
      m = min(m, f(p, k));
    }
  }
  cout << m << endl;
  return 0;
}
