#include <bits/stdc++.h>
using namespace std;
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string lowerCase = "abcdefghijklmnopqrstuvwxyz";
string upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int fx[] = {+1, +0, -1, +0};
const int fy[] = {+0, +1, +0, -1};
bool flag;
char c[55][55];
bool visited[55][55];
int n, r1, c1, r2, c2;
set<pair<int, int> > source, destination;
void dfs(int x, int y) {
  visited[x][y] = 1;
  if (!flag)
    source.insert(make_pair(x, y));
  else
    destination.insert(make_pair(x, y));
  for (int i = 0; i < 4; i++) {
    int a = x + fx[i];
    int b = y + fy[i];
    if ((a >= 1 && a <= n && b >= 1 && b <= n) && !visited[a][b] &&
        c[a][b] == '0') {
      dfs(a, b);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> r1 >> c1;
  cin >> r2 >> c2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) cin >> c[i][j];
  }
  dfs(r1, c1);
  flag = true;
  auto it1 = source.find(make_pair(r1, c1));
  auto it2 = source.find(make_pair(r2, c2));
  int mn = INT_MAX;
  if (it1 != source.end() && it2 != source.end()) {
    mn = 0;
  } else {
    dfs(r2, c2);
    for (auto i = source.begin(); i != source.end(); i++) {
      for (auto j = destination.begin(); j != destination.end(); j++) {
        int x = i->first - j->first;
        int y = i->second - j->second;
        int dist = x * x + y * y;
        if (dist < mn) mn = dist;
      }
    }
  }
  cout << mn << endl;
  return 0;
}
