#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long maxn = (long long)60;
const long double EPS = 1e-9;
const long long INF = (long long)1e18 + 18;
const long long mod = (long long)1e9 + 7;
long long n, r1, c1, r2, c2;
long long a[maxn][maxn], used[maxn][maxn][2];
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
vector<pair<long long, long long>> v1, v2;
void go(long long x, long long y, long long type) {
  if (x < 0 || x >= n || y < 0 || y >= n) return;
  if (a[x][y] == 1) return;
  if (used[x][y][type] == 1) return;
  used[x][y][type] = 1;
  if (type == 0) {
    v1.push_back({x, y});
    for (long long i = 0; i < 4; i++) {
      go(x + dx[i], y + dy[i], type);
    }
  } else {
    v2.push_back({x, y});
    for (long long i = 0; i < 4; i++) {
      go(x + dx[i], y + dy[i], type);
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> r1 >> c1 >> r2 >> c2;
  for (long long i = 0; i < n; i++) {
    string in;
    cin >> in;
    for (long long j = 0; j < n; j++) {
      a[i][j] = in[j] - '0';
    }
  }
  memset(used, 0, sizeof used);
  ;
  go(r1 - 1, c1 - 1, 0);
  go(r2 - 1, c2 - 1, 1);
  long long ans = INF;
  for (auto [x1, asdgh] : v1) {
    for (auto [x2, y2] : v2) {
      ans = min(ans, (x1 - x2) * (x1 - x2) + (asdgh - y2) * (asdgh - y2));
    }
  }
  cout << ans << '\n';
  return 0;
}
