Bob is about to visit N of his friends' houses. Each house is depicted as a point on the plane. The length of roads between two houses i and j is defined as .
There's a matrix M filled with 1s and 0s. denotes that it is possible to travel to House from House and vice versa. If , it is not possible to travel to House from House and vice versa.
Now, Bob wants to visit all the houses, but the sum of length of all roads traveled should be maximum. There's one more restriction, he can use a maximum of roads. Before starting the trip, he would like to know what is the maximum distance he can travel which satisfies all the given conditions. If it is not possible to visit all the houses, print out -1.
Input Format:
The first line contains N, the number of houses Bob wants to visit.
N lines follow, each contains two space separated integers and which denotes the of the house on the plane.
Again, N lines follow, each contains N space separated integers, each being 0 or 1, denoting the matrix M.
Output Format:
Print the required answer in one line.
Input Constraints:
Note:
One road can be traversed any number of times , but its contribution would be taken only once. Also, there's large input data, please use faster i/o methods.
This is the image for the given sample. Every house also has a road of length 0 to itself. We can use at max 2 roads which would be from (1,2) -> (8,8) -> (4,5). The total cost is 85 + 25 = 110.