#include <bits/stdc++.h>
using namespace std;
const int32_t N = 54;
int32_t n;
int32_t dx[] = {0, 1, 0, -1};
int32_t dy[] = {1, 0, -1, 0};
bool g[N][N];
int32_t med[N][N];
queue<pair<int32_t, int32_t> > q;
void ff(int32_t r, int32_t c, int32_t l) {
  while (!q.empty()) q.pop();
  q.push({r, c});
  med[r][c] = l;
  while (!q.empty()) {
    pair<int32_t, int32_t> cr = q.front();
    q.pop();
    for (int32_t i = 0; i < 4; i++) {
      pair<int32_t, int32_t> nx;
      nx.first = cr.first + dx[i];
      nx.second = cr.second + dy[i];
      if (g[nx.first][nx.second]) continue;
      if (nx.first <= 0 || nx.first > n || nx.second < 1 || nx.second > n)
        continue;
      if (med[nx.first][nx.second] == med[cr.first][cr.second]) continue;
      med[nx.first][nx.second] = med[cr.first][cr.second];
      q.push(nx);
    }
  }
}
vector<pair<int32_t, int32_t> > v1, v2;
signed main() {
  cin >> n;
  int32_t x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int32_t i = 1; i <= n; i++) {
    for (int32_t j = 1; j <= n; j++) {
      char c;
      cin >> c;
      if (c == '0')
        g[i][j] = 0;
      else
        g[i][j] = 1;
    }
  }
  ff(x1, y1, 1);
  ff(x2, y2, 2);
  if (med[x1][y1] == med[x2][y2]) {
    cout << 0;
    return 0;
  }
  for (int32_t i = 1; i <= n; i++)
    for (int32_t j = 1; j <= n; j++)
      if (med[i][j] == 1)
        v1.push_back({i, j});
      else if (med[i][j] == 2)
        v2.push_back({i, j});
  int32_t ans = LONG_MAX;
  for (pair<int32_t, int32_t> p1 : v1)
    for (pair<int32_t, int32_t> p2 : v2)
      ans = min(ans, (p1.first - p2.first) * (p1.first - p2.first) +
                         (p1.second - p2.second) * (p1.second - p2.second));
  cout << ans;
}
