#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
const double eps = 0.1;
int n;
int vis[maxn][maxn];
int ok = 0;
int n1, m1, n2, m2;
char mp[maxn][maxn];
int str[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<pair<int, int> > v[2];
void dfs(int x, int y, int fg) {
  if (x == n2 && y == m2 && !fg) {
    ok = 1;
    return;
  }
  if (ok) return;
  vis[x][y] = 1;
  v[fg].push_back(pair<int, int>(x, y));
  for (int i = 0; i < 4; i++) {
    int dx = x + str[i][0];
    int dy = y + str[i][1];
    if (dx >= 1 && dx <= n && dy >= 1 && dy <= n && mp[dx][dy] == '0' &&
        !vis[dx][dy]) {
      dfs(dx, dy, fg);
    }
  }
}
int main() {
  scanf("%d%d%d%d%d", &n, &n1, &m1, &n2, &m2);
  for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  dfs(n1, m1, 0);
  if (ok) {
    puts("0");
    return 0;
  }
  dfs(n2, m2, 1);
  long long ans = 1LL << 60;
  for (int i = 0; i < v[0].size(); i++) {
    for (int j = 0; j < v[1].size(); j++) {
      ans = min(ans, 1LL * (v[0][i].first - v[1][j].first) *
                             (v[0][i].first - v[1][j].first) +
                         (v[0][i].second - v[1][j].second) *
                             (v[0][i].second - v[1][j].second));
    }
  }
  printf("%lld\n", ans);
  return 0;
}
