#include <bits/stdc++.h>
using namespace std;
int n, r1, c1, r2, c2;
vector<string> v;
void f(int x, int i, int j) {
  if (i >= 0 && i < n && j >= 0 && j < n && v[i][j] == '0') {
    v[i][j] = x;
    f(x, i - 1, j);
    f(x, i + 1, j);
    f(x, i, j - 1);
    f(x, i, j + 1);
  }
}
int main() {
  cin >> n >> r1 >> c1 >> r2 >> c2;
  v.resize(n);
  for (int i = 0; i < n; ++i) cin >> v[i];
  f('2', r1 - 1, c1 - 1);
  if (v[r2 - 1][c2 - 1] == '2')
    cout << "0\n";
  else {
    f('3', r2 - 1, c2 - 1);
    int best = (r1 - r2) * (r1 - r2) + (c1 - c2) * (c1 - c2);
    for (int i1 = 0; i1 < n; ++i1) {
      for (int j1 = 0; j1 < n; ++j1) {
        if (v[i1][j1] != '2') continue;
        for (int i2 = 0; i2 < n; ++i2) {
          for (int j2 = 0; j2 < n; ++j2) {
            if (v[i2][j2] != '3') continue;
            int d = (i1 - i2) * (i1 - i2) + (j1 - j2) * (j1 - j2);
            if (d < best) best = d;
          }
        }
      }
    }
    cout << best << endl;
  }
  return 0;
}
