#include <bits/stdc++.h>
using namespace std;
int n, cost;
int di[4] = {0, -1, 0, 1};
int dj[4] = {-1, 0, 1, 0};
char grid[51][51];
int vis[51][51];
pair<int, int> pi;
pair<int, int> pf;
vector<pair<int, int>> ili;
vector<pair<int, int>> ilf;
void dfs(int r, int c, vector<pair<int, int>>* island) {
  vis[r][c] = 1;
  (*island).push_back({r, c});
  for (int v = 0; v < 4; v++) {
    int i = r + di[v];
    int j = c + dj[v];
    if (i > 0 && i <= n && j > 0 && j <= n) {
      if (grid[i][j] == '0' && !vis[i][j]) {
        dfs(i, j, island);
      }
    }
  }
}
int main() {
  cin >> n;
  cin >> pi.first >> pi.second;
  cin >> pf.first >> pf.second;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> grid[i][j];
    }
  }
  dfs(pi.first, pi.second, &ili);
  if (vis[pf.first][pf.second]) {
    cout << "0" << endl;
  } else {
    dfs(pf.first, pf.second, &ilf);
    int less = (int)INFINITY;
    for (int i = 0; i < (int)ili.size(); i++) {
      for (int j = 0; j < (int)ilf.size(); j++) {
        cost = pow((ili[i].first - ilf[j].first), 2) +
               pow((ili[i].second - ilf[j].second), 2);
        if (cost < less && cost) {
          less = cost;
        }
      }
    }
    cout << less << endl;
  }
  return 0;
}
