#include <bits/stdc++.h>
using namespace std;
vector<string> mat;
vector<pair<int, int> > firstComp;
int posX[] = {1, 0, -1, 0};
int posY[] = {0, 1, 0, -1};
long long res;
bool isValid(int x, int y) {
  return x >= 0 && x < mat.size() && y >= 0 && y < mat.size();
}
void dfsFind(int x, int y) {
  if (mat[x][y] == '1') {
    return;
  }
  mat[x][y] = '1';
  firstComp.push_back({x, y});
  for (int nx, ny, i = 0; i < 4; i++) {
    nx = x + posX[i];
    ny = y + posY[i];
    if (isValid(nx, ny)) {
      dfsFind(nx, ny);
    }
  }
}
void dfsSolve(int x, int y) {
  if (mat[x][y] == '1') {
    return;
  }
  mat[x][y] = '1';
  long long res1, res2;
  for (auto par : firstComp) {
    res1 = par.first - x;
    res2 = par.second - y;
    res = min(res, (res1 * res1) + (res2 * res2));
  }
  for (int nx, ny, i = 0; i < 4; i++) {
    nx = x + posX[i];
    ny = y + posY[i];
    if (isValid(nx, ny)) {
      dfsSolve(nx, ny);
    }
  }
}
int main() {
  for (int n; cin >> n;) {
    pair<int, int> ori, dest;
    cin >> ori.first >> ori.second;
    cin >> dest.first >> dest.second;
    ori.first--, ori.second--;
    dest.first--, dest.second--;
    res = 0x7fffffff;
    mat = vector<string>(n);
    for (int i = 0; i < n; i++) {
      cin >> mat[i];
    }
    firstComp.clear();
    dfsFind(ori.first, ori.second);
    if (mat[dest.first][dest.second] == '0')
      dfsSolve(dest.first, dest.second);
    else
      res = 0;
    cout << res << "\n";
  }
  return 0;
}
