#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 50;
int n, r1, c1, r2, c2;
char symbol;
bool gr[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
set<pair<int, int>> first, second;
int status, ans = INT_MAX;
void DFS(int r, int c) {
  if (status == 1)
    first.insert({r, c});
  else
    second.insert({r, c});
  vis[r][c] = 1;
  if (r - 1 >= 0 && vis[r - 1][c] == 0 && gr[r - 1][c] == 0) DFS(r - 1, c);
  if (r + 1 < n && vis[r + 1][c] == 0 && gr[r + 1][c] == 0) DFS(r + 1, c);
  if (c - 1 >= 0 && vis[r][c - 1] == 0 && gr[r][c - 1] == 0) DFS(r, c - 1);
  if (c + 1 < n && vis[r][c + 1] == 0 && gr[r][c + 1] == 0) DFS(r, c + 1);
}
int main() {
  cin >> n >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;
  r2--;
  c2--;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> symbol;
      if (symbol == '0')
        gr[i][j] = 0;
      else
        gr[i][j] = 1;
    }
  status = 1;
  DFS(r1, c1);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) vis[i][j] = 0;
  status = 2;
  DFS(r2, c2);
  if (first == second) {
    cout << 0 << endl;
    return 0;
  }
  for (auto f : first)
    for (auto s : second) {
      int x1 = f.first;
      int y1 = f.second;
      int x2 = s.first;
      int y2 = s.second;
      int dist = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
      ans = min(dist, ans);
    }
  cout << ans << endl;
  return 0;
}
