문제
Farmer John's cows, conveniently numbered , are standing in a line (). The th cow has a breed identifier in the range , with . The cows need your help to figure out how to best transmit a message from cow to cow .
It takes time to transmit a message from cow to cow . However, not all breeds are willing to communicate with each other, as described by a matrix , where if a cow of breed is willing to transmit a message to a cow of breed , and otherwise. It is not necessarily true that , and it may even be the case that if cows of breed are unwilling to communicate with each-other.
Please determine the minimum amount of time needed to transmit the message.
입력
The first line contains and .
The next line contains space-separated integers .
The next lines describe the matrix . Each consists of a string of bits, being the th bit of the th string from the top.
출력
Print a single integer giving the minimum amount of time needed. If it is impossible to transmit the message from cow to cow , then output .
예제 입력 1
5 4
1 4 2 3 4
1010
0001
0110
0100
예제 출력 1
6
점수
Test cases 1-5 satisfy .Test cases 6-13 satisfy no additional constraints.
코드를 제출하려면 로그인이 필요합니다.
로그인