#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>> v, vector<vector<int>>& v1,
         set<vector<int>>& start, int r1, int c1, int n) {
  if (r1 < 1 || r1 > n || c1 < 1 || c1 > n) return;
  if (v1[r1][c1] || v[r1][c1]) return;
  vector<int> ve;
  ve.push_back(r1);
  ve.push_back(c1);
  start.insert(ve);
  v1[r1][c1] = 1;
  dfs(v, v1, start, r1, c1 + 1, n);
  dfs(v, v1, start, r1, c1 - 1, n);
  dfs(v, v1, start, r1 + 1, c1, n);
  dfs(v, v1, start, r1 - 1, c1, n);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  string s[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  vector<vector<int>> v(n + 1, vector<int>(n + 1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      v[i][j] = s[i][j - 1] - '0';
    }
  }
  vector<vector<int>> v1(n + 1, vector<int>(n + 1, 0));
  vector<vector<int>> v2(n + 1, vector<int>(n + 1, 0));
  set<vector<int>> start;
  set<vector<int>> last;
  dfs(v, v1, start, r1, c1, n);
  dfs(v, v2, last, r2, c2, n);
  int result = INT_MAX;
  for (auto it1 = start.begin(); it1 != start.end(); it1++) {
    vector<int> t1 = *it1;
    for (auto it2 = last.begin(); it2 != last.end(); it2++) {
      vector<int> t2 = *it2;
      result = min(result, int(pow(t1[0] - t2[0], 2) + pow(t1[1] - t2[1], 2)));
    }
  }
  cout << result;
  return 0;
}
