#include <bits/stdc++.h>
using namespace std;
const long long maxn = 51;
long long n, r1, c1, r2, c2;
vector<string> arr;
set<pair<long long, long long>> S, D;
void floodfill(long long x, long long y, long long s) {
  if (x >= n || x < 0) return;
  if (y >= n || y < 0) return;
  if (arr[x][y] == '1') return;
  if (arr[x][y] == '#') return;
  if (!s) {
    pair<long long, long long> p = {x, y};
    S.insert(p);
  } else {
    pair<long long, long long> p = {x, y};
    D.insert(p);
  }
  arr[x][y] = '#';
  floodfill(x + 1, y, s);
  floodfill(x - 1, y, s);
  floodfill(x, y + 1, s);
  floodfill(x, y - 1, s);
}
int main() {
  cin >> n >> r1 >> c1 >> r2 >> c2;
  for (long long i = 1; i <= n; i++) {
    string s;
    cin >> s;
    arr.push_back(s);
  }
  floodfill(r1 - 1, c1 - 1, 0);
  floodfill(r2 - 1, c2 - 1, 1);
  long long ans = 1e18;
  for (auto& u : S)
    for (auto& l : D)
      ans = min(ans, (u.first - l.first) * (u.first - l.first) +
                         (u.second - l.second) * (u.second - l.second));
  if (ans == 1e18) ans = 0;
  cout << ans << endl;
  return 0;
}
