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.

<image> 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 5
00001
11111
00111
00110
00110


Output


10


Input


3
1 3
3 1
010
101
010


Output


8

Note

In the first sample, a tunnel between cells (1, 4) and (4, 5) should be created. The cost of doing so is (1-4)^2 + (4-5)^2 = 10, which is optimal. This way, Alice could walk from (1, 1) to (1, 4), use the tunnel from (1, 4) to (4, 5), and lastly walk from (4, 5) to (5, 5).

In the second sample, clearly a tunnel between cells (1, 3) and (3, 1) needs to be created. The cost of doing so is (1-3)^2 + (3-1)^2 = 8.
# problem: https://codeforces.com/problemset/problem/1130/C

from queue import PriorityQueue

# complexity:
# -time O(ElogK) - > Elog(K^2) where K is a number of vertices from one of the connected components
# -space O(V^2*E)


def neighbours(n, actual):
    position = []
    positions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    for x, y in positions:
        new_x = actual//n + x
        new_y = actual % n + y
        # condition: is in the array
        if new_x >= 0 and new_x < n and new_y >= 0 and new_y < n:
            position.append((new_x, new_y))

    return position


def distance(a, b, n):
    ax, ay = a // n, a % n
    bx, by = b // n, b % n
    return int((ax-bx)**2 + (ay-by)**2)


# to memorize, if is needed tunel between two fields- firstly I'll run DFS alg. to find all connected components
def DFS_group(array, n, group_connected_components, group_number, actual):
    group_connected_components[actual] = group_number
    for new_x, new_y in neighbours(n, actual):
        # condition: is in the array and coord is not a water and also wasn't visited before
        if new_x >= 0 and new_x < n and new_y >= 0 and new_y < n and int(array[new_x][new_y]) == 0 and group_connected_components[new_x*n+new_y % n] is None:
            DFS_group(array, n, group_connected_components,
                      group_number, new_x*n+new_y % n)


# if elements are in same connected commponents => cost of edge is 0, otherwise cost of tunel from exercise formula
# def new_tunels(groups, group_a_b, tunels, n, actual, index):
#     for coord in range(index, len(group_a_b)):
#         # same connected components <=> endge weight 0
#         coord = group_a_b[coord]
#         if groups[coord] == groups[actual]:
#             tunels[actual].append((coord, 0))
#             tunels[coord].append((actual, 0))
#         else:
#             d = distance(actual, coord, n)
#             tunels[actual].append((coord, d))
#             tunels[coord].append((actual, d))


# # edge relaxing in Dijkstra algorithm
# def relax(distances, u, v, w):
#     if distances[v] > distances[u]+w:
#         distances[v] = distances[u]+w
#         return True
#     else:
#         return False


# # Dijkstra algorithm to find best connection
# def Dijkstra_distance(graph, n, b, e):
#     distances = [float("inf")]*n
#     distances[b] = 0
#     p_queue = PriorityQueue()
#     for v, w in graph[b]:
#         # tuples in priority queue: (edge weight, first end of edge, second end of edge)
#         p_queue.put((w, b, v))
#     while not p_queue.empty():
#         w, u, v = p_queue.get()
#         if relax(distances, u, v, w):
#             # adding edges connected to ending vertex of relaxed edge
#             for i, w in graph[v]:
#                 if i != u:
#                     p_queue.put((w, v, i))
#     return distances[e]


# def connect():
#     n = int(input())
#     begining = [int(el) for el in input().split()]
#     b = (begining[0]-1)*n+begining[1]-1
#     end = [int(el) for el in input().split()]
#     e = (end[0]-1)*n + end[1]-1
#     array = [input() for _ in range(n)]

#     # numbering groups of connected components
#     groups = [None] * (n**2)
#     group_number = -1
#     for i in range(n**2):
#         if groups[i] == None and int(array[i//n][i % n]) != 1:
#             group_number += 1
#             DFS_group(array, n, groups, group_number, i)

#     # now program memorized connected components -> between vertices of the same group edge weight == 0
#     # if vertices are in the same connected component group, distance between is equal 0
#     if groups[e] == groups[b]:
#         return 0

#     # need to filter only elements which became into one of two groups vertices:
#     # -connected components with begining vertex
#     # -connected components with ending vertex
#     group_a_b = []
#     for i in range(n**2):
#         if groups[i] == groups[b] or groups[i] == groups[e]:
#             group_a_b.append(i)

#     # creating connections only between every vertices from connecting components of starting point and CC to every vertex in end of connecting components
#     graph = [list() for _ in range(n**2)]
#     for index, actual in enumerate(group_a_b):
#         new_tunels(groups, group_a_b, graph, n, actual, index)
#     return Dijkstra_distance(graph, n**2, b, e)


# print(connect())


# becouse on codeforces is Runtime error using my first alg. so i decided to reduce a bit complexity-> becouse I can maxial build one bridge, so result is min(A[i],B[j]), where A[i] is in beginning CC group and B[j] in edging vertex CC group

def connect():
    n = int(input())
    begining = [int(el) for el in input().split()]
    b = (begining[0]-1)*n+begining[1]-1
    end = [int(el) for el in input().split()]
    e = (end[0]-1)*n + end[1]-1
    array = [input() for _ in range(n)]
    
    # for big N
    counter = 0
    for string in array:
        for letter in string:
            if letter == "1":
                counter += 1
    if counter <= 2:
        return 0

    groups = [None] * (n**2)
    group_number = -1
    for i in range(n**2):
        if groups[i] == None and int(array[i//n][i % n]) != 1:
            group_number += 1
            DFS_group(array, n, groups, group_number, i)
    if groups[e] == groups[b]:
        return 0

    A = []
    B = []
    for i in range(n**2):
        if groups[i] == groups[b]:
            A.append(i)
        elif groups[i] == groups[e]:
            B.append(i)

    cheapest_tunel = float("inf")

    for i in range(len(A)):
        for j in range(len(B)):
            cheapest_tunel = min(cheapest_tunel, distance(A[i], B[j], n))
    return cheapest_tunel


print(connect())