#include <bits/stdc++.h>
using namespace std;
const int sz = 55;
int n, r1, c1, r2, c2, ans = 1000000000;
string s[sz];
bool visited[sz][sz];
vector<pair<int, int> > v;
void dfs(int x, int y, bool flag) {
  if (x < 1 || x > n || y < 1 || y > n || s[x][y] == '1' ||
      visited[x][y] == true) {
    return;
  }
  visited[x][y] = true;
  if (flag == true) {
    v.push_back(make_pair(x, y));
  } else {
    for (int i = 0; i < v.size(); i++) {
      int cost = (v[i].first - x) * (v[i].first - x) +
                 (v[i].second - y) * (v[i].second - y);
      ans = min(ans, cost);
    }
  }
  dfs(x - 1, y, flag);
  dfs(x + 1, y, flag);
  dfs(x, y - 1, flag);
  dfs(x, y + 1, flag);
}
int main() {
  cin >> n;
  cin >> r1 >> c1 >> r2 >> c2;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = " " + s[i];
  }
  memset(visited, false, sizeof(visited));
  dfs(r2, c2, true);
  memset(visited, false, sizeof(visited));
  dfs(r1, c1, false);
  cout << ans << endl;
  return 0;
}
