human
Give me a Python code to find the minimum possible cost of creating at most one tunnel so that Alice could travel from (r_1, c_1) to (r_2, c_2) given the following:
Alice lives on a flat planet that can be modeled as a square grid of size n × n, with rows and columns enumerated from 1 to n. We represent the cell at the intersection of row r and column c with ordered pair (r, c). Each cell in the grid is either land or water.

 An example planet with n = 5. It also appears in the first sample test.

Alice resides in land cell (r_1, c_1). She wishes to travel to land cell (r_2, c_2). At any moment, she may move to one of the cells adjacent to where she is—in one of the four directions (i.e., up, down, left, or right).

Unfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice's trip may be impossible.

To help Alice, you plan to create at most one tunnel between some two land cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells (r_s, c_s) and (r_t, c_t) is (r_s-r_t)^2 + (c_s-c_t)^2.

For now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from (r_1, c_1) to (r_2, c_2). If no tunnel needs to be created, the cost is 0.

Input

The first line contains one integer n (1 ≤ n ≤ 50) — the width of the square grid.

The second line contains two space-separated integers r_1 and c_1 (1 ≤ r_1, c_1 ≤ n) — denoting the cell where Alice resides.

The third line contains two space-separated integers r_2 and c_2 (1 ≤ r_2, c_2 ≤ n) — denoting the cell to which Alice wishes to travel.

Each of the following n lines contains a string of n characters. The j-th character of the i-th such line (1 ≤ i, j ≤ n) is 0 if (i, j) is land or 1 if (i, j) is water.

It is guaranteed that (r_1, c_1) and (r_2, c_2) are land.

Output

Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from (r_1, c_1) to (r_2, c_2).

Examples

Input

5
1 1
5...
#comp
assistant
#Here is Python code to find the minimum possible cost of creating at most one tunnel so that Alice could travel from (r_1, c_1) to (r_2, c_2).
lexity - O(n^2)
import math
import sys

sys.setrecursionlimit(2600)

class UnionFind():
  def __init__(self, num_elements):
    self.parent = list(range(num_elements))
    self.rank = [1] * num_elements

  def union(self, a, b):
    par_a = self.find(a)
    par_b = self.find(b)
    
    if par_a == par_b:
      return
    
    if self.rank[par_a] > self.rank[par_b]:
      self.parent[par_b] = par_a    
      self.rank[par_a] = self.rank[par_a] + self.rank[par_b]
    else:
      self.parent[par_a] = par_b    
      self.rank[par_b] = self.rank[par_a] + self.rank[par_b]
  
  def find(self, a):
    if self.parent[a] == a:
      return a
    par_a = self.find(self.parent[a])
    curr_parent = self.parent[a]
    if par_a != curr_parent:
      self.rank[curr_parent] -= self.rank[a]
      self.parent[a] = par_a

    return par_a

n = int(input())
r1, c1 = map(lambda x:int(x)-1, input().split())
r2, c2 = map(lambda x:int(x)-1, input().split())

arr = []
for i in range(n):
  arr.append(input())

def dfs():
  seen = set()
  directions = [(-1, 0), (0, -1), (0, 1), (1, 0)]
  uf = UnionFind(n*n)

  def find_index(i, j):
    return i*n+j
  
  def _dfs(i, j):
    if (i, j) in seen:
      return
    seen.add((i, j)) 
    if arr[i][j] == '1':
      return
    for direction in directions:
      ni, nj = i+direction[0], j+direction[1]
      if ni >= 0 and ni < n and nj >= 0 and nj < n and (ni, nj) not in seen and arr[ni][nj] == '0':
        uf.union(i*n+j, ni*n+nj)
        _dfs(ni, nj)

  for i in range(n):
    for j in range(n):
      _dfs(i, j)

  if uf.find(r1*n+c1) == uf.find(r2*n+c2):
    return 0 
  parent1 = uf.find(r1*n+c1)
  parent2 = uf.find(r2*n+c2)
  min_distance = math.inf
  set_a = set()
  set_b = set()

  for i in range(n):
    for j in range(n):
      parent = uf.find(find_index(i, j))
      if parent == parent1:
        set_a.add((i, j))
      elif parent == parent2:
        set_b.add((i, j))
  
  for el1 in set_a:
    for el2 in set_b:
      dist = pow(el1[0]-el2[0], 2) + pow(el1[1]-el2[1], 2)
     ...