Miracle Tree
On one of the branches of the magic tree, a gardener has cultivated various types of fruits (such as bananas, oranges, etc.) and is now ready to harvest them. He can pick two adjacent fruits, and when he does, a new fruit immediately grows in the space between them. The type of this new fruit is entirely determined by the types of the two fruits that were picked.
Since each picking reduces the total number of fruits by 1, eventually, there will be only one fruit left, which cannot be picked. Your task is to determine the possible type(s) of this final fruit.
Input
The first line of the input file contains two integers N and k (1 ≤ N ≤ 500, 1 ≤ k ≤ 10), where N is the number of fruits on the branch, and k is the number of different fruit types that can grow on the magic tree. The second line lists N integers, representing the types of fruits growing consecutively on the branch, with each type ranging from 1 to k. Each of the following k lines contains k integers, which specify the rules for the growth of new fruits: the j-th number in the i-th line indicates the type of fruit that will grow if two consecutive fruits are picked, where the first fruit is of type i and the second is of type j.
Output
On a single line of the output file, print the types of fruits that could potentially be the last one remaining on the branch of the magic tree, listed in ascending order.