#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
const int N = 51;
const int oo = 1e9;
const double pi = acos(-1);
int n;
bool vis[N][N];
vector<string> g;
pair<int, int> a, b;
vector<pair<int, int>> va, vb;
int dr[4] = {1, -1, 0, 0};
int dc[4] = {0, 0, 1, -1};
void DFS(int r, int c) {
  vis[r][c] = true;
  for (int i = 0; i < 4; ++i) {
    int newr = r + dr[i];
    int newc = c + dc[i];
    if (newr < 0 || newr == n || newc < 0 || newc == n ||
        g[newr][newc] == '1' || vis[newr][newc])
      continue;
    DFS(newr, newc);
  }
}
int main() {
  cin >> n;
  cin >> a.first >> a.second;
  cin >> b.first >> b.second;
  --a.first;
  --a.second;
  --b.first;
  --b.second;
  g.resize(n);
  for (int i = 0; i < n; ++i) cin >> g[i];
  DFS(a.first, a.second);
  if (vis[b.first][b.second]) {
    puts("0");
    return 0;
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (vis[i][j]) va.push_back({i, j});
  memset(vis, 0, sizeof vis);
  DFS(b.first, b.second);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (vis[i][j]) vb.push_back({i, j});
  int ans = oo;
  for (auto x : va) {
    for (auto y : vb) {
      int dis = pow(x.first - y.first, 2) + pow(x.second - y.second, 2);
      ans = min(ans, dis);
    }
  }
  cout << ans << endl;
  return 0;
}
