#include <bits/stdc++.h>
using namespace std;
char maze[55][55];
int vis[2][55][55];
int n;
void dfs(int x, int y, int val) {
  if (vis[val][x][y]) return;
  vis[val][x][y] = 1;
  if (maze[x + 1][y] == '0') dfs(x + 1, y, val);
  if (maze[x - 1][y] == '0') dfs(x - 1, y, val);
  if (maze[x][y + 1] == '0') dfs(x, y + 1, val);
  if (maze[x][y - 1] == '0') dfs(x, y - 1, val);
}
int main() {
  cin >> n;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> maze[i][j];
    }
  dfs(sx, sy, 0);
  dfs(ex, ey, 1);
  long long ans = 1e18;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      if (vis[0][i][j]) {
        for (long long k = 1; k <= n; k++)
          for (long long l = 1; l <= n; l++) {
            if (vis[1][k][l]) {
              ans = min(ans, (l - j) * (l - j) + (k - i) * (k - i));
            }
          }
      }
    }
  }
  cout << ans << '\n';
}
