#include <bits/stdc++.h>
char map[55][55];
int main() {
  int n, ans = 2147483647;
  std::pair<int, int> s, t;
  std::queue<std::pair<int, int> > q;
  std::vector<std::pair<int, int> > vec1, vec2;
  scanf("%d %d %d %d %d", &n, &s.first, &s.second, &t.first, &t.second);
  for (int i = 1; i <= n; ++i) scanf("%s", map[i] + 1);
  vec1.push_back(s), map[s.first][s.second] = '2', q.push(s);
  while (!q.empty()) {
    std::pair<int, int> p = q.front();
    q.pop();
    std::pair<int, int> temp = std::make_pair(p.first, p.second + 1);
    if (temp.second <= n && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec1.push_back(temp), q.push(temp);
    temp = std::make_pair(p.first, p.second - 1);
    if (temp.second >= 0 && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec1.push_back(temp), q.push(temp);
    temp = std::make_pair(p.first + 1, p.second);
    if (temp.first <= n && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec1.push_back(temp), q.push(temp);
    temp = std::make_pair(p.first - 1, p.second);
    if (temp.first >= 0 && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec1.push_back(temp), q.push(temp);
  }
  if (map[t.first][t.second] == '2') {
    putchar('0');
    return 0;
  }
  vec2.push_back(t), map[t.first][t.second] = '2', q.push(t);
  while (!q.empty()) {
    std::pair<int, int> p = q.front();
    q.pop();
    std::pair<int, int> temp = std::make_pair(p.first, p.second + 1);
    if (temp.second <= n && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec2.push_back(temp), q.push(temp);
    temp = std::make_pair(p.first, p.second - 1);
    if (temp.second >= 0 && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec2.push_back(temp), q.push(temp);
    temp = std::make_pair(p.first + 1, p.second);
    if (temp.first <= n && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec2.push_back(temp), q.push(temp);
    temp = std::make_pair(p.first - 1, p.second);
    if (temp.first >= 0 && map[temp.first][temp.second] == '0')
      map[temp.first][temp.second] = '2', vec2.push_back(temp), q.push(temp);
  }
  for (auto x1 : vec1)
    for (auto x2 : vec2)
      ans =
          std::min(ans, (x2.first - x1.first) * (x2.first - x1.first) +
                            (x2.second - x1.second) * (x2.second - x1.second));
  printf("%d\n", ans);
  return 0;
}
