#include <bits/stdc++.h>
using namespace std;
int n;
int arr[52][53];
bool vis[52][53];
vector<pair<int, int> > v[2];
void dfs(int r, int c, int f) {
  if (r >= n || c >= n || r < 0 || c < 0) return;
  if (arr[r][c] == 1) return;
  if (!vis[r][c]) {
    vis[r][c] = 1;
    v[f].push_back({r, c});
    dfs(r + 1, c, f);
    dfs(r, c + 1, f);
    dfs(r, c - 1, f);
    dfs(r - 1, c, f);
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  for (int i = 0; i < int(n); i++) {
    string s;
    cin >> s;
    for (int j = 0; j < int(n); j++)
      if (s[j] == '0')
        arr[i][j] = 0;
      else
        arr[i][j] = 1;
  }
  dfs(r1 - 1, c1 - 1, 0);
  memset(vis, 0, sizeof(vis));
  dfs(r2 - 1, c2 - 1, 1);
  int a = v[1].size(), b = v[0].size();
  int ans = INT_MAX;
  cerr << "a"
       << ": " << a << " | "
       << "b"
       << ": " << b << "\n";
  for (int i = 0; i < int(a); i++) {
    for (int j = 0; j < int(b); j++) {
      r1 = v[0][j].first;
      c1 = v[0][j].second;
      r2 = v[1][i].first;
      c2 = v[1][i].second;
      int tmp = (r1 - r2) * (r1 - r2) + (c1 - c2) * (c1 - c2);
      ans = min(ans, tmp);
    }
  }
  cout << ans << "\n";
  return 0;
}
