#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 50;
const long long INF = 1e10;
vector<string> mat(N);
int n, r1, c1, r2, c2;
vector<vector<bool>> visited(N, vector<bool>(N));
void dfs(int i, int j, vector<pair<int, int>> &s) {
  visited[i][j] = true;
  s.push_back({i, j});
  if (i + 1 < n and !visited[i + 1][j] and mat[i + 1][j] == '0')
    dfs(i + 1, j, s);
  if (i - 1 >= 0 and !visited[i - 1][j] and mat[i - 1][j] == '0')
    dfs(i - 1, j, s);
  if (j + 1 < n and !visited[i][j + 1] and mat[i][j + 1] == '0')
    dfs(i, j + 1, s);
  if (j - 1 >= 0 and !visited[i][j - 1] and mat[i][j - 1] == '0')
    dfs(i, j - 1, s);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  vector<pair<int, int>> s, f;
  cin >> n >> r1 >> c1 >> r2 >> c2;
  for (int i = 0; i < n; i++) cin >> mat[i];
  dfs(r1 - 1, c1 - 1, s);
  if (!visited[r2 - 1][c2 - 1]) dfs(r2 - 1, c2 - 1, f);
  if (f.size() == 0) {
    cout << 0;
    return 0;
  }
  int ans = INT_MAX;
  for (auto i : s) {
    for (auto j : f)
      ans = min(ans, (i.first - j.first) * (i.first - j.first) +
                         (i.second - j.second) * (i.second - j.second));
  }
  cout << ans;
}
