#include <bits/stdc++.h>
using namespace std;
bool board[51][51];
bool visited[50][50];
int n;
bool checkPos(int x, int y);
vector<pair<int, int>> dfs(int x, int y) {
  vector<pair<int, int>> v;
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    pair<int, int> s = q.front();
    v.push_back(s);
    q.pop();
    x = s.first, y = s.second;
    if (checkPos(x - 1, y)) {
      q.push(make_pair(x - 1, y));
    }
    if (checkPos(x + 1, y)) {
      q.push(make_pair(x + 1, y));
    }
    if (checkPos(x, y + 1)) {
      q.push(make_pair(x, y + 1));
    }
    if (checkPos(x, y - 1)) {
      q.push(make_pair(x, y - 1));
    }
  }
  return v;
}
bool checkPos(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > n) {
    return false;
  }
  if (visited[x][y]) {
    return false;
  }
  if (board[x][y]) {
    visited[x][y] = true;
    return true;
  }
  return false;
}
int main() {
  cin >> n;
  int x1, x2, y1, y2;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= n; j++) {
      visited[i][j] = false;
      if (s[j - 1] == '0') {
        board[i][j] = true;
      } else
        board[i][j] = false;
    }
  }
  vector<pair<int, int>> start;
  vector<pair<int, int>> end;
  start = dfs(x1, y1);
  end = dfs(x2, y2);
  int maxDist = 5000;
  for (pair<int, int> p : start) {
    for (pair<int, int> p2 : end) {
      maxDist = min(maxDist, (int)(pow(p.first - p2.first, 2)) +
                                 (int)(pow(p.second - p2.second, 2)));
    }
  }
  cout << maxDist;
}
