#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> *G, int v, vector<bool> &visited) {
  visited[v] = true;
  for (int u : G[v]) {
    if (!visited[u]) dfs(G, u, visited);
  }
}
void connections(vector<int> *G, int v, vector<bool> &visited,
                 vector<int> &component) {
  visited[v] = true;
  component.push_back(v);
  for (int u : G[v]) {
    if (!visited[u]) connections(G, u, visited, component);
  }
}
int main() {
  int n, r1, c1, r2, c2, answer = INT_MAX;
  cin >> n >> r1 >> c1 >> r2 >> c2;
  string grid[n];
  vector<int> G[n * n];
  vector<bool> visited(n * n);
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (grid[i][j] == '0' && j > 0 && grid[i][j - 1] == '0') {
        G[i * n + j].push_back(i * n + j - 1);
      }
      if (grid[i][j] == '0' && j + 1 < n && grid[i][j + 1] == '0') {
        G[i * n + j].push_back(i * n + j + 1);
      }
      if (grid[i][j] == '0' && i > 0 && grid[i - 1][j] == '0') {
        G[i * n + j].push_back(i * n - n + j);
      }
      if (grid[i][j] == '0' && i + 1 < n && grid[i + 1][j] == '0') {
        G[i * n + j].push_back(i * n + n + j);
      }
    }
  }
  dfs(G, (--r1) * n + (--c1), visited);
  if (visited[(--r2) * n + (--c2)]) {
    cout << 0;
  } else {
    vector<int> component1, component2;
    visited = vector<bool>(n * n);
    connections(G, r1 * n + c1, visited, component1);
    connections(G, r2 * n + c2, visited, component2);
    for (int d1 : component1) {
      for (int d2 : component2) {
        answer = min(answer, (d1 / n - d2 / n) * (d1 / n - d2 / n) +
                                 (d1 % n - d2 % n) * (d1 % n - d2 % n));
      }
    }
    cout << answer;
  }
}
