#include <bits/stdc++.h>
using namespace std;
int n, a, b, r, c, used[100][100], viz[100][100], ans = INT_MAX;
int g[100][100];
vector<pair<int, int> > s, t;
void DFS1(int x, int y) {
  if (used[x][y] == 1) return;
  used[x][y] = 1;
  s.push_back(make_pair(x, y));
  if (g[x + 1][y] == 0 && x + 1 <= n) DFS1(x + 1, y);
  if (g[x - 1][y] == 0 && x - 1 >= 1) DFS1(x - 1, y);
  if (g[x][y + 1] == 0 && y + 1 <= n) DFS1(x, y + 1);
  if (g[x][y - 1] == 0 && y - 1 >= 1) DFS1(x, y - 1);
}
void DFS2(int x, int y) {
  if (viz[x][y] == 1) return;
  viz[x][y] = 1;
  t.push_back(make_pair(x, y));
  if (g[x + 1][y] == 0 && x + 1 <= n) DFS2(x + 1, y);
  if (g[x - 1][y] == 0 && x - 1 >= 1) DFS2(x - 1, y);
  if (g[x][y + 1] == 0 && y + 1 <= n) DFS2(x, y + 1);
  if (g[x][y - 1] == 0 && y - 1 >= 1) DFS2(x, y - 1);
}
int main() {
  scanf("%d%d%d%d%d", &n, &a, &b, &r, &c);
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      char u;
      cin >> u;
      if (u == '1')
        g[k][i] = 1;
      else
        g[k][i] = 0;
    }
  }
  DFS1(a, b);
  DFS2(r, c);
  if (t == s) {
    cout << 0;
    return 0;
  }
  for (int k = 0; k < s.size(); k++) {
    for (int i = 0; i < t.size(); i++) {
      int u = (s[k].first - t[i].first) * (s[k].first - t[i].first) +
              (s[k].second - t[i].second) * (s[k].second - t[i].second);
      ans = min(ans, u);
    }
  }
  printf("%d", ans);
  return 0;
}
