#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > vec[2501];
bool visited[50][50];
int subgroup[50][50];
int a[50][50];
int n;
void expand(int i, int j, int subgroup_num) {
  if (i < 0 || j < 0 || i >= n || j >= n) {
    return;
  }
  if (visited[i][j]) {
    return;
  }
  if (a[i][j] == 0) {
    return;
  }
  visited[i][j] = true;
  subgroup[i][j] = subgroup_num;
  vec[subgroup_num].push_back(make_pair(i, j));
  expand(i + 1, j, subgroup_num);
  expand(i - 1, j, subgroup_num);
  expand(i, j + 1, subgroup_num);
  expand(i, j - 1, subgroup_num);
  return;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      visited[i][j] = false;
    }
  }
  int r_one, c_one, r_two, c_two;
  cin >> r_one >> c_one;
  cin >> r_two >> c_two;
  r_one -= 1;
  c_one -= 1;
  r_two -= 1;
  c_two -= 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char x;
      cin >> x;
      int y = x - 48;
      a[i][j] = 1 - y;
    }
  }
  int subgroup_num = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (!visited[i][j] && a[i][j] == 1) {
        subgroup[i][j] = subgroup_num;
        vec[subgroup_num].push_back(make_pair(i, j));
        expand(i, j, subgroup_num);
        subgroup_num++;
      }
    }
  }
  if (subgroup[r_one][c_one] == subgroup[r_two][c_two]) {
    cout << "0\n";
    return 0;
  } else {
    int first = subgroup[r_one][c_one];
    int second = subgroup[r_two][c_two];
    int temp = INT_MAX;
    for (int i = 0; i < vec[first].size(); i++) {
      int first_i = vec[first][i].first, first_j = vec[first][i].second;
      for (int j = 0; j < vec[second].size(); j++) {
        int second_i = vec[second][j].first, second_j = vec[second][j].second;
        int dist = pow(first_i - second_i, 2) + pow(second_j - first_j, 2);
        if (dist < temp) {
          temp = dist;
        }
      }
    }
    cout << temp << "\n";
  }
  return 0;
}
