#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename Tem1, typename... Args>
void err(istream_iterator<string> it, Tem1 a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
template <typename Tem2>
bool big_cmp(Tem2 a, Tem2 b) {
  return a > b;
}
const long long inf = 1ll << 60;
char IG[51][51];
int grid[51][51];
int n;
bool ingrid(int r, int c) { return r >= 0 && r < n && c >= 0 && c < n; }
void bfs(pair<int, int> at, int col) {
  queue<pair<int, int> > qq;
  qq.push(at);
  while (!qq.empty()) {
    int r = qq.front().first, c = qq.front().second;
    qq.pop();
    if (!ingrid(r, c)) continue;
    if (grid[r][c] != 0) continue;
    grid[r][c] = col;
    qq.push({r + 1, c});
    qq.push({r - 1, c});
    qq.push({r, c + 1});
    qq.push({r, c - 1});
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  int r0, c0, r1, c1;
  cin >> r0 >> c0 >> r1 >> c1;
  r0--;
  c0--;
  r1--;
  c1--;
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (n); j++) cin >> IG[i][j], grid[i][j] = IG[i][j] - '0';
  bfs({r0, c0}, 2);
  if (grid[r1][c1] == 2) {
    cout << 0 << '\n';
    return 0;
  }
  bfs({r1, c1}, 3);
  vector<vector<pair<int, int> > > v(2);
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (n); j++) {
      if (grid[i][j] > 1) {
        v[grid[i][j] - 2].push_back({i, j});
      }
    }
  }
  int re = 10000000;
  for (pair<int, int> e1 : v[0]) {
    for (pair<int, int> e2 : v[1]) {
      re = min(re, (e1.first - e2.first) * (e1.first - e2.first) +
                       (e1.second - e2.second) * (e1.second - e2.second));
    }
  }
  cout << re << endl;
}
