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,…,pmp1,p2,…,pm of mm vertexes; for each 1≤i<m1≤i<m there is an arc from pipi to pi+1pi+1.

Define the sequence v1,v2,…,vkv1,v2,…,vk of kk vertexes as good, if vv is a subsequence of ppv1=p1v1=p1vk=pmvk=pm, and pp is one of the shortest paths passing through the vertexes v1v1vkvk 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.

 

Input

The first line contains a single integer nn (2≤n≤1002≤n≤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 (2≤m≤1062≤m≤106) — the number of vertexes in the path.

The next line contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pi≤n1≤pi≤n) — the sequence of vertexes in the path. It is guaranteed that for any 1≤i<m1≤i<m there is an arc from pipi to pi+1pi+1.

Output

In the first line output a single integer kk (2≤k≤m2≤k≤m) — the length of the shortest good subsequence. In the second line output kk integers v1v1vkvk (1≤vi≤n1≤vi≤n) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.

Examples

Input
4
0110
0010
0001
1000
4
1 2 3 4
Output
3
1 2 4
Input
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
Output
11
1 2 4 2 4 2 4 2 4 2 4
Input
3
011
101
110
7
1 2 3 1 3 2 1
Output
7
1 2 3 1 3 2 1
Input
4
0110
0001
0001
1000
3
1 2 4
Output
2
1 4

题意:按照所给序列在图上走最短路,要求输出最短的子序列,使得在图上走的最短路不变(即某点去掉<不是去掉图中这个点>, 不影响上个点到下个点的距离)

思路

首先用floyd求一下任意两点之间的最短路,在逐一判断。

(本题将题目给的最短序列 存到p数组中)

对于一个序列中第i个数,我们要判断它能不能去掉,就是看必须要出现在序列中的最后一个点cnt 与 第i 和 第i+1个的点的关系。

若w[cnt][p[i]] + w[p[i]] [p[i+1]] <=w[cnt][p[i+1]],则第i个点肯定能去掉(因为这样走最短路 一定会经过i的 cnt->p[i]->p[i+1]);反之,不可以去掉。

为什么要更新cnt ?

比如当前答案序列是1,2 , 现在要判断3能否加入序列,根据题意我们要看从2按最短路径走能否先经过3,而不是判断1按最短路径走能否经过3。

所以判断加入一个元素,是判断答案序列的末尾元素能否以最短路方式经过该点

#include <bits/stdc++.h>

using namespace std;

const int maxn = 205;

const int INF = 0x7f7f7f7f;
const int N = 1e6 + 5;


int g[maxn][maxn];
int p[N];
bool f[N];


int main() {
	int n , m;
	ios::sync_with_stdio(0);
	cin >> n;
	for(int i = 1 ;i <= n ; i++) { //数据初始化
		for(int j = 1 ; j <= n ; j++) {
			char t;
			cin >> t;
			g[i][j] = t - '0';
			if(!g[i][j])g[i][j] = INF;
		}
	}
	for(int k = 1 ; k <= n ; k++) {
		for(int i = 1 ; i <= n ; i++) {
			for(int j = 1 ; j <= n ; j++) {
				if(g[i][k] < INF && g[k][j] < INF)
				g[i][j] = min(g[i][k] + g[k][j] , g[i][j]);
			}
		}
	}

	cin >> m;
	int t = m; //t记录未删除的数目
	for(int i = 1 ; i <= m ; i++)cin >> p[i];
	int cnt = p[1];  //序列第一个开始遍历 
	for(int i = 2 ; i <= m - 1 ; i++) { //序列第一个 最后一个不能删
		if(g[cnt][p[i]] + g[p[i]][p[i + 1]] <= g[cnt][p[i + 1]] && cnt != p[i+1]) {  // !!!第二个条件易忘
			f[i] = 1;  //等于一就表示被删除了
			t--;     
		}
		else cnt = p[i];  //不能删 就更新判断答案序列的末尾元素
	}
	cout << t << "\n";
	for(int i = 1 ; i <= m ; i++) {
		if(f[i])continue;
		cout << p[i] << " ";
	}
}