#include <bits/stdc++.h>
using namespace std;
char mat[55][55];
int n;
int A[] = {-1, 0, 1, 0};
int B[] = {0, 1, 0, -1};
vector<pair<int, int> > ilha1, ilha2;
void flood_fill(int i, int j, bool prima) {
  if (i < 0 or j < 0 or i >= n or j >= n or mat[i][j] != '0') return;
  mat[i][j] = '1';
  if (prima)
    ilha1.push_back({i, j});
  else
    ilha2.push_back({i, j});
  for (int k = 0; k < 4; k++) {
    flood_fill(i + A[k], j + B[k], prima);
  }
}
int main() {
  pair<int, int> p1, p2;
  scanf("%d", &n);
  scanf("%d%d%d%d", &p1.first, &p1.second, &p2.first, &p2.second);
  p1.first--;
  p1.second--;
  p2.first--;
  p2.second--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf(" %c", &mat[i][j]);
    }
  }
  flood_fill(p1.first, p1.second, true);
  if (mat[p2.first][p2.second] == '1') {
    puts("0");
    return 0;
  }
  flood_fill(p2.first, p2.second, false);
  int ans = INT_MAX;
  for (int i = 0; i < (int)ilha1.size(); i++) {
    for (int j = 0; j < (int)ilha2.size(); j++) {
      int a = abs(ilha1[i].first - ilha2[j].first);
      int b = abs(ilha1[i].second - ilha2[j].second);
      ans = min(ans, a * a + b * b);
    }
  }
  cout << ans << endl;
  return 0;
}
