#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  long long ans = (a * b) / (gcd(a, b));
  return ans;
}
long long n;
bool vis[55][55], mat[55][55];
vector<pair<long long, long long> > v1, v2;
void fun(long long x, long long y, bool b) {
  if (x > n || y > n || x < 1 || y < 1 || vis[x][y] == 1 || mat[x][y] == 1)
    return;
  vis[x][y] = 1;
  if (b == 0) {
    v1.push_back({x, y});
  } else {
    v2.push_back({x, y});
  }
  fun(x, y + 1, b);
  fun(x, y - 1, b);
  fun(x + 1, y, b);
  fun(x - 1, y, b);
}
signed main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  long long TESTS = 1;
  while (TESTS--) {
    cin >> n;
    long long r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    memset(vis, 0, sizeof(vis));
    for (long long i = 1; i < n + 1; i++) {
      string s;
      cin >> s;
      for (long long j = 0; j < n; j++) {
        mat[i][j + 1] = s[j] - '0';
      }
    }
    long long ans = 123456789012345;
    fun(r1, c1, 0);
    memset(vis, 0, sizeof(vis));
    fun(r2, c2, 1);
    for (auto i : v1) {
      for (auto j : v2) {
        long long x1 = i.first, y1 = i.second, x2 = j.first, y2 = j.second;
        long long p = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
        ans = min(ans, p);
      }
    }
    cout << ans << endl;
  }
  return (0);
}
