#include <bits/stdc++.h>
using namespace std;
char a[100][100];
int v[2][100][100];
int n;
std::vector<pair<int, int>> fuck[2];
void dfs(int r, int c, int x) {
  if (r > 0 && r <= n && c > 0 && c <= n) {
    if (a[r][c] == '0') {
      v[x][r][c] = 1;
      fuck[x].push_back({r, c});
      if (!v[x][r + 1][c]) dfs(r + 1, c, x);
      if (!v[x][r - 1][c]) dfs(r - 1, c, x);
      if (!v[x][r][c + 1]) dfs(r, c + 1, x);
      if (!v[x][r][c - 1]) dfs(r, c - 1, x);
    }
  }
}
int main() {
  int x1, x2, y1, y2;
  cin >> n;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; j++) cin >> a[i][j];
  }
  dfs(x1, y1, 0);
  dfs(x2, y2, 1);
  int ans = 1e6;
  for (auto i : fuck[0]) {
    for (auto z : fuck[1]) {
      int t = pow(abs(i.first - z.first), 2) + pow(abs(i.second - z.second), 2);
      ans = min(ans, t);
    }
  }
  cout << ans;
}
