#include <bits/stdc++.h>
using namespace std;
bool isVowel(char c) {
  return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
bool isLower(char c) { return 97 <= c && c <= 122; }
bool prime[1001];
void sieve() {
  fill(prime, prime + 1000, 1);
  prime[0] = prime[1] = 0;
  for (long long int i = 2; i < 1000; i += 1) {
    if (prime[i]) {
      for (long long int j = i * 2; j < 1000; j += i) prime[j] = 0;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  queue<pair<int, int>> v1, v2;
  map<pair<int, int>, bool> vis1, vis2;
  v1.push(make_pair(x1, y1));
  vis1[v1.front()] = true;
  while (!v1.empty()) {
    int px = v1.front().first;
    int py = v1.front().second;
    v1.pop();
    if (px == x2 && py == y2) {
      cout << 0;
      return 0;
    }
    if (px + 1 < n && !vis1[make_pair(px + 1, py)] && a[px + 1][py] == '0')
      v1.push(make_pair(px + 1, py)), vis1[make_pair(px + 1, py)] = true;
    if (px - 1 >= 0 && !vis1[make_pair(px - 1, py)] && a[px - 1][py] == '0')
      v1.push(make_pair(px - 1, py)), vis1[make_pair(px - 1, py)] = true;
    if (py + 1 < n && !vis1[make_pair(px, py + 1)] && a[px][py + 1] == '0')
      v1.push(make_pair(px, py + 1)), vis1[make_pair(px, py + 1)] = true;
    if (py - 1 >= 0 && !vis1[make_pair(px, py - 1)] && a[px][py - 1] == '0')
      v1.push(make_pair(px, py - 1)), vis1[make_pair(px, py - 1)] = true;
  }
  v2.push(make_pair(x2, y2));
  vis2[v2.front()] = true;
  while (!v2.empty()) {
    int px = v2.front().first;
    int py = v2.front().second;
    v2.pop();
    if (px + 1 < n && !vis2[make_pair(px + 1, py)] && a[px + 1][py] == '0')
      v2.push(make_pair(px + 1, py)), vis2[make_pair(px + 1, py)] = true;
    if (px - 1 >= 0 && !vis2[make_pair(px - 1, py)] && a[px - 1][py] == '0')
      v2.push(make_pair(px - 1, py)), vis2[make_pair(px - 1, py)] = true;
    if (py + 1 < n && !vis2[make_pair(px, py + 1)] && a[px][py + 1] == '0')
      v2.push(make_pair(px, py + 1)), vis2[make_pair(px, py + 1)] = true;
    if (py - 1 >= 0 && !vis2[make_pair(px, py - 1)] && a[px][py - 1] == '0')
      v2.push(make_pair(px, py - 1)), vis2[make_pair(px, py - 1)] = true;
  }
  int ans = INT_MAX;
  for (auto e : vis1) {
    for (auto ee : vis2) {
      if (!e.second || !ee.second) {
        continue;
      }
      auto p1 = e.first;
      auto p2 = ee.first;
      int dist = (p1.first - p2.first) * (p1.first - p2.first) +
                 (p1.second - p2.second) * (p1.second - p2.second);
      ans = min(ans, dist);
    }
  }
  cout << ans;
}
