#P1204C. Anna, Svyatoslav and Maps

    ID: 2292 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>dpgraphsgreedyshortest paths*1700

Anna, Svyatoslav and Maps

No submission language available for this problem.

Description

The main characters have been omitted to be short.

You are given a directed unweighted graph without loops with nn vertexes and a path in it (that path is not necessary simple) given by a sequence p1,p2,,pmp_1, p_2, \ldots, p_m of mm vertexes; for each 1i<m1 \leq i < m there is an arc from pip_i to pi+1p_{i+1}.

Define the sequence v1,v2,,vkv_1, v_2, \ldots, v_k of kk vertexes as good, if vv is a subsequence of pp, v1=p1v_1 = p_1, vk=pmv_k = p_m, and pp is one of the shortest paths passing through the vertexes v1v_1, \ldots, vkv_k in that order.

A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements. It is obvious that the sequence pp is good but your task is to find the shortest good subsequence.

If there are multiple shortest good subsequences, output any of them.

The first line contains a single integer nn (2n1002 \le n \le 100) — the number of vertexes in a graph.

The next nn lines define the graph by an adjacency matrix: the jj-th character in the ii-st line is equal to 11 if there is an arc from vertex ii to the vertex jj else it is equal to 00. It is guaranteed that the graph doesn't contain loops.

The next line contains a single integer mm (2m1062 \le m \le 10^6) — the number of vertexes in the path.

The next line contains mm integers p1,p2,,pmp_1, p_2, \ldots, p_m (1pin1 \le p_i \le n) — the sequence of vertexes in the path. It is guaranteed that for any 1i<m1 \leq i < m there is an arc from pip_i to pi+1p_{i+1}.

In the first line output a single integer kk (2km2 \leq k \leq m) — the length of the shortest good subsequence. In the second line output kk integers v1v_1, \ldots, vkv_k (1vin1 \leq v_i \leq n) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.

Input

The first line contains a single integer nn (2n1002 \le n \le 100) — the number of vertexes in a graph.

The next nn lines define the graph by an adjacency matrix: the jj-th character in the ii-st line is equal to 11 if there is an arc from vertex ii to the vertex jj else it is equal to 00. It is guaranteed that the graph doesn't contain loops.

The next line contains a single integer mm (2m1062 \le m \le 10^6) — the number of vertexes in the path.

The next line contains mm integers p1,p2,,pmp_1, p_2, \ldots, p_m (1pin1 \le p_i \le n) — the sequence of vertexes in the path. It is guaranteed that for any 1i<m1 \leq i < m there is an arc from pip_i to pi+1p_{i+1}.

Output

In the first line output a single integer kk (2km2 \leq k \leq m) — the length of the shortest good subsequence. In the second line output kk integers v1v_1, \ldots, vkv_k (1vin1 \leq v_i \leq n) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.

Samples

Sample Input 1

4
0110
0010
0001
1000
4
1 2 3 4

Sample Output 1

3
1 2 4

Sample Input 2

4
0110
0010
1001
1000
20
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

Sample Output 2

11
1 2 4 2 4 2 4 2 4 2 4

Sample Input 3

3
011
101
110
7
1 2 3 1 3 2 1

Sample Output 3

7
1 2 3 1 3 2 1

Sample Input 4

4
0110
0001
0001
1000
3
1 2 4

Sample Output 4

2
1 4

Note

Below you can see the graph from the first example:

The given path is passing through vertexes 11, 22, 33, 44. The sequence 1241-2-4 is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes 11, 22 and 44 in that order is 12341-2-3-4. Note that subsequences 141-4 and 1341-3-4 aren't good because in both cases the shortest path passing through the vertexes of these sequences is 1341-3-4.

In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes.

In the fourth example, the paths 1241-2-4 and 1341-3-4 are the shortest paths passing through the vertexes 11 and 44.