#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
char m[55][55];
vector<pii> g[2];
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
int n;
void canGo(int rs, int cs, int id) {
  queue<pii> q;
  q.push({rs, cs});
  while (!q.empty()) {
    int r = q.front().first;
    int c = q.front().second;
    g[id].push_back({r, c});
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int rr = r + dr[i];
      int cc = c + dc[i];
      if (rr > 0 && rr <= n && cc > 0 && cc <= n && m[rr][cc] != '1') {
        m[rr][cc] = '1';
        q.push({rr, cc});
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  int rs, cs, re, ce;
  scanf("%d%d%d%d", &rs, &cs, &re, &ce);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf(" %c", &m[i][j]);
    }
  }
  canGo(rs, cs, 0);
  canGo(re, ce, 1);
  int ans = 50000;
  for (int i = 0; i < g[0].size(); ++i) {
    for (int j = 0; j < g[1].size(); ++j) {
      int x = g[0][i].first - g[1][j].first;
      int y = g[0][i].second - g[1][j].second;
      ans = min(ans, x * x + y * y);
    }
  }
  printf("%d\n", ans);
  return 0;
}
