#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int visited[100][100];
long long n;
string str[100];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
vector<pair<long long, long long>> vp1, vp2;
int check(long long x, long long y) {
  if (x < 0 || x > n - 1 || y < 0 || y > n - 1 || str[x][y] != '0' ||
      visited[x][y])
    return 0;
  return 1;
}
void dfs(long long r1, long long c1) {
  visited[r1][c1] = 1;
  vp1.push_back({r1, c1});
  for (int i = 0; i < 4; i++) {
    int newx = r1 + dx[i];
    int newy = c1 + dy[i];
    if (check(newx, newy)) dfs(newx, newy);
  }
}
void solve() {
  vector<long long> v;
  long long a, r1, r2, c1, c2;
  cin >> n;
  cin >> r1 >> c1 >> r2 >> c2;
  for (long long i = 0; i < n; i++) cin >> str[i];
  dfs(r1 - 1, c1 - 1);
  vp2 = vp1;
  vp1.clear();
  dfs(r2 - 1, c2 - 1);
  long long mn = 1e18;
  for (long long i = 0; i < vp1.size(); i++) {
    for (long long j = 0; j < vp2.size(); j++) {
      long long diff =
          (vp1[i].first - vp2[j].first) * (vp1[i].first - vp2[j].first) +
          (vp1[i].second - vp2[j].second) * (vp1[i].second - vp2[j].second);
      if (diff < mn) mn = diff;
    }
  }
  cout << mn << "\n";
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  solve();
  return 0;
}
