#include <bits/stdc++.h>
using namespace std;
vector<vector<char>> m;
vector<vector<bool>> used;
vector<pair<int, int>> t;
long long cur_min = 1000000000;
bool dfs1(int r, int c, int tr, int tc) {
  used[r][c] = true;
  t.push_back(make_pair(r, c));
  if (r == tr && c == tc) return true;
  if (r > 0 && m[r - 1][c] == '0' && !used[r - 1][c]) dfs1(r - 1, c, tr, tc);
  if (c > 0 && m[r][c - 1] == '0' && !used[r][c - 1]) dfs1(r, c - 1, tr, tc);
  if (r < m.size() - 1 && m[r + 1][c] == '0' && !used[r + 1][c])
    dfs1(r + 1, c, tr, tc);
  if (c < m.size() - 1 && m[r][c + 1] == '0' && !used[r][c + 1])
    dfs1(r, c + 1, tr, tc);
  return false;
}
void dfs2(int r, int c) {
  used[r][c] = true;
  for (auto ti : t) {
    if (cur_min >
        (r - ti.first) * (r - ti.first) + (c - ti.second) * (c - ti.second))
      cur_min =
          (r - ti.first) * (r - ti.first) + (c - ti.second) * (c - ti.second);
  }
  if (r > 0 && m[r - 1][c] == '0' && !used[r - 1][c]) dfs2(r - 1, c);
  if (c > 0 && m[r][c - 1] == '0' && !used[r][c - 1]) dfs2(r, c - 1);
  if (r < m.size() - 1 && m[r + 1][c] == '0' && !used[r + 1][c]) dfs2(r + 1, c);
  if (c < m.size() - 1 && m[r][c + 1] == '0' && !used[r][c + 1]) dfs2(r, c + 1);
}
int main() {
  int n;
  cin >> n;
  m.assign(n, vector<char>(n));
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;
  r2--;
  c2--;
  cin.get();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cin >> m[i][j];
    cin.get();
  }
  used.assign(n, vector<bool>(n, false));
  if (dfs1(r2, c2, r1, c1))
    cout << 0;
  else {
    dfs2(r1, c1);
    cout << cur_min;
  }
  return 0;
}
