#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18, MAX = 55, mod = 1000000007;
int add[2][4] = {1, -1, 0, 0, 0, 0, 1, -1};
pair<int, int> parent[MAX][MAX];
pair<int, int> root(pair<int, int> x) {
  if (parent[x.first][x.second] == x) return x;
  parent[x.first][x.second] = root(parent[x.first][x.second]);
  return parent[x.first][x.second];
}
void Union(pair<int, int> x, pair<int, int> y) {
  pair<int, int> a = root(x);
  pair<int, int> b = root(y);
  if (a == b) return;
  parent[a.first][a.second] = b;
}
int getCost(pair<int, int> from, pair<int, int> to) {
  int cost = (to.first - from.first) * (to.first - from.first);
  cost += (to.second - from.second) * (to.second - from.second);
  return cost;
}
void initDSU(int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      parent[i][j] = {i, j};
    }
  }
}
int main() {
  int n, r1, c1, r2, c2;
  vector<string> v;
  string s;
  cin >> n;
  cin >> r1 >> c1;
  cin >> r2 >> c2;
  initDSU(n);
  for (int i = 0; i < n; i++) {
    cin >> s;
    v.push_back(s);
  }
  r1--;
  c1--;
  r2--;
  c2--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < 4; k++) {
        int p = i + add[0][k];
        int q = j + add[1][k];
        if (p < 0 || q < 0 || p >= n || q >= n) continue;
        if (v[i][j] == v[p][q]) Union({i, j}, {p, q});
      }
    }
  }
  pair<int, int> srcRoot = root({r1, c1});
  pair<int, int> desRoot = root({r2, c2});
  if (srcRoot == desRoot) {
    cout << 0 << endl;
    return 0;
  }
  vector<pair<int, int>> src, des;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      pair<int, int> currRoot = root({i, j});
      if (currRoot == srcRoot)
        src.push_back({i, j});
      else if (currRoot == desRoot)
        des.push_back({i, j});
    }
  }
  int minCost = getCost({r1, c1}, {r2, c2});
  for (pair<int, int> from : src) {
    for (pair<int, int> to : des) {
      minCost = min(minCost, getCost(from, to));
    }
  }
  cout << minCost << endl;
  return 0;
}
