#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
const long long maxn = 50, mod = 1e9 + 7;
string s[maxn];
long long mat[maxn][maxn];
int n;
void dfs(long long i, long long j, vector<pair<long long, long long> > &v) {
  if (i == n || j == n || i < 0 || j < 0) return;
  if (s[i][j] == '1') return;
  if (mat[i][j] != -1) return;
  mat[i][j] = 1;
  v.push_back(make_pair(i, j));
  dfs(i + 1, j, v);
  dfs(i, j + 1, v);
  dfs(i - 1, j, v);
  dfs(i, j - 1, v);
}
long long dist(pair<long long, long long> a, pair<long long, long long> b) {
  long long x = a.first - b.first;
  long long y = a.second - b.second;
  return x * x + y * y;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  for (long long i = 0; i < (long long)(n); ++i) {
    cin >> s[i];
  }
  vector<pair<long long, long long> > he, ho;
  memset(mat, -1, sizeof(mat));
  dfs(a, b, he);
  memset(mat, -1, sizeof(mat));
  dfs(c, d, ho);
  long long ans = LONG_LONG_MAX;
  if (he.front() == ho.front()) {
    cout << 0;
    return 0;
  }
  for (long long i = 0; i < he.size(); i++) {
    for (long long j = 0; j < ho.size(); j++) {
      ans = min(ans, dist(he[i], ho[j]));
    }
  }
  cout << ans;
  return 0;
}
