#include <bits/stdc++.h>
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (x < y) x = y;
}
using namespace std;
vector<pair<int, int>> v[2];
int a[51][51], n;
bool vis[51][51];
void dp(int i, int j, int k) {
  if (i < 0 || j < 0 || i > n || j > n) return;
  if (a[i][j] == 0 && vis[i][j] == false) {
    v[k].push_back(make_pair(i, j));
    vis[i][j] = true;
  }
  if (a[i + 1][j] == 0 && vis[i + 1][j] == false) dp(i + 1, j, k);
  if (a[i][j + 1] == 0 && vis[i][j + 1] == false) dp(i, j + 1, k);
  if (a[i - 1][j] == 0 && vis[i - 1][j] == false) dp(i - 1, j, k);
  if (a[i][j - 1] == 0 && vis[i][j - 1] == false) dp(i, j - 1, k);
}
void solve() {
  int r1, c1, r2, c2, ans = INT_MAX, i, j;
  cin >> n >> r1 >> c1 >> r2 >> c2;
  for (i = 0; i < 51; i++)
    for (j = 0; j < 51; j++) a[i][j] = -1;
  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (j = 0; j < (int)s.size(); j++) {
      a[i][j] = s[j] - '0';
      vis[i][j] = false;
    }
  }
  dp(r1 - 1, c1 - 1, 0);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) vis[i][j] = false;
  dp(r2 - 1, c2 - 1, 1);
  for (auto i : v[0]) {
    for (auto j : v[1])
      amin(ans, (i.first - j.first) * (i.first - j.first) +
                    (i.second - j.second) * (i.second - j.second));
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
