#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> vis;
vector<pair<int, int>> glob;
void dfs(map<pair<int, int>, vector<pair<int, int>>> &graph, pair<int, int> x) {
  glob.push_back(x);
  vis[x] = 1;
  for (int i = 0; i < graph[x].size(); i++)
    if (!vis[graph[x][i]]) dfs(graph, graph[x][i]);
}
int main() {
  map<pair<int, int>, vector<pair<int, int>>> graph;
  int n;
  cin >> n;
  string str[n];
  int x1, y1;
  cin >> x1 >> y1;
  int x2, y2;
  cin >> x2 >> y2;
  for (int i = 0; i < n; i++) cin >> str[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (str[i][j] == '0') {
        if (i + 1 < n && str[i + 1][j] == '0') {
          graph[make_pair(i, j)].push_back(make_pair(i + 1, j));
          graph[make_pair(i + 1, j)].push_back(make_pair(i, j));
        }
        if (i - 1 >= 0 && str[i - 1][j] == '0') {
          graph[make_pair(i, j)].push_back(make_pair(i - 1, j));
          graph[make_pair(i - 1, j)].push_back(make_pair(i, j));
        }
        if (j + 1 < n && str[i][j + 1] == '0') {
          graph[make_pair(i, j)].push_back(make_pair(i, j + 1));
          graph[make_pair(i, j + 1)].push_back(make_pair(i, j));
        }
        if (j - 1 >= 0 && str[i][j - 1] == '0') {
          graph[make_pair(i, j)].push_back(make_pair(i, j - 1));
          graph[make_pair(i, j - 1)].push_back(make_pair(i, j));
        }
      }
  dfs(graph, make_pair(x1 - 1, y1 - 1));
  vector<pair<int, int>> hold1 = glob;
  glob.clear();
  vis.clear();
  dfs(graph, make_pair(x2 - 1, y2 - 1));
  vector<pair<int, int>> hold2 = glob;
  glob.clear();
  vis.clear();
  int m = 100000;
  for (int i = 0; i < hold1.size(); i++)
    for (int j = 0; j < hold2.size(); j++) {
      int temp = (hold1[i].first - hold2[j].first) *
                     (hold1[i].first - hold2[j].first) +
                 (hold1[i].second - hold2[j].second) *
                     (hold1[i].second - hold2[j].second);
      m = min(m, temp);
    }
  cout << m << endl;
}
