#include <bits/stdc++.h>
using namespace std;
bool comparator(pair<int, int> a, pair<int, int> b) {}
int n;
int findRoot(int a, int b, int tree[50][50]) {
  if (tree[a][b] == a * n + b) {
    return tree[a][b];
  }
  return findRoot(tree[a][b] / n, tree[a][b] % n, tree);
}
void findUnion(int a1, int b1, int a2, int b2, int tree[50][50]) {
  int rootA = findRoot(a1, b1, tree);
  int rootB = findRoot(a2, b2, tree);
  tree[rootA / n][rootA % n] = rootB;
}
int findDist(pair<int, int> a, pair<int, int> b) {
  return pow((a.first - b.first), 2) + pow((a.second - b.second), 2);
}
int findClosest(int val1, int val2, int b[50][50]) {
  int cost = 1e7;
  vector<pair<int, int> > a, c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (b[i][j] == val1) {
        a.push_back(make_pair(i, j));
      }
      if (b[i][j] == val2) {
        c.push_back(make_pair(i, j));
      }
    }
  }
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < c.size(); j++) {
      int dist = findDist(a[i], c[j]);
      if (dist < cost) {
        cost = dist;
      }
    }
  }
  return cost;
}
int main() {
  cin >> n;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  string a[50];
  int b[50][50];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < n; j++) {
      b[i][j] = i * n + j;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i > 0 && a[i][j] == a[i - 1][j]) {
        findUnion(i, j, i - 1, j, b);
      }
      if (j > 0 && a[i][j] == a[i][j - 1]) findUnion(i, j, i, j - 1, b);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      b[i][j] = findRoot(i, j, b);
    }
  }
  cout << findClosest(b[r1 - 1][c1 - 1], b[r2 - 1][c2 - 1], b);
  return 0;
}
