#include <bits/stdc++.h>
using namespace std;
int dr[] = {0, 0, 1, -1}, n;
int dc[] = {1, -1, 0, 0};
char adjMatrix[51][51];
bool vis[51][51];
void dfs(int r, int c, vector<pair<int, int> >& s) {
  vis[r][c] = 1;
  s.push_back(make_pair(r, c));
  for (int i = 0; i < 4; i++) {
    int r2 = r + dr[i], c2 = c + dc[i];
    if (r2 >= 1 && r2 <= n && c2 >= 1 && c2 <= n && !vis[r2][c2] &&
        adjMatrix[r2][c2] == '0')
      dfs(r2, c2, s);
  }
}
int main() {
  int r1, c1, r2, c2;
  cin >> n >> r1 >> c1 >> r2 >> c2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) cin >> adjMatrix[i][j];
  }
  vector<pair<int, int> > s, e;
  dfs(r1, c1, s);
  dfs(r2, c2, e);
  if (e.size() == 0)
    cout << "0" << endl;
  else {
    int ans = INT_MAX;
    for (auto x : s) {
      for (auto y : e) {
        int k = ((x.first - y.first) * (x.first - y.first)) +
                ((x.second - y.second) * (x.second - y.second));
        if (k < ans) ans = k;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
