Given an n*n grid matrix with rows and columns numbered from 1 to n, the grid coordinates of row i and column j are (i,j)
Each square is either flat or a trap.
The traveler starts in squares (r1, c1) and goes to squares (r2, c2).
The starting point and destination are guaranteed to be flat ground.
The traveler can move up, down, left, or right one space at a time.
We make sure the traveler doesn't step out of the matrix or into a trap while moving.
But traveler may not make it to his destination because of traps.
Fortunately, he can choose any two flat squares and build a teleporter array on them.
Teleport array allows traveler to teleport freely between two flat squares.
However, building a transport array is a significant cost.
The cost of establishing a transmission array at squares (rs, cs) and (rt,ct) is (rs − rt)2+(cs − ct)2.
Calculate the minimum cost of establishing a teleport array to enable travelers to successfully reach their destination.
If traveler can walk directly to his destination without having to build a teleport array, the cost is zero.
Note that no more than one transport array can be established.
The first line contains the integer n, representing the size of the matrix.
The second row contains two integers r1 and c1, indicating that the starting grid coordinates are (r1,c1).
The third row contains two integers, r2,c2, indicating that the destination grid coordinates are (r2,c2).
The next n lines, each containing a string of '0' or '1' of length n, where the jth character in line i is 0, indicating that square (i,j) is flat, and 1, indicating that square (i,j) is a trap.
(1≤n≤50,1≤r1,c1,r2,c2≤n。)
Output an integer that represents the minimum cost required to ensure that the trip will succeed.
3 1 1 3 3 011 000 100
0