#include<bits/stdc++.h> usingnamespace std; int n,m,p[1000005],a[105][105]; char x; vector<int> ans; intmain(){ ios::sync_with_stdio(false);cin.tie(0),cout.tie(0); cin>>n; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++)a[i][j]=1926817;//初始化 for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){ cin>>x; if(x=='1')a[i][j]=1;//读入图 } for (int i=1;i<=n;i++)a[i][i]=0; for (int k=1;k<=n;k++) for (int i=1;i<=n;i++) for (int j=1;j<=n;j++)a[i][j]=min(a[i][j],a[i][k]+a[k][j]);//Floyd求最短路 cin>>m; for (int i=1;i<=m;i++)cin>>p[i]; int i=1,j=2;ans.push_back(p[1]);//p[1]是必到的 for (;i<=m&&j<=m;){ while(j<m&&a[p[i]][p[j+1]]>=a[p[i]][p[j]]+1)j++;//j<m保证了下标不越界,我们要找能满足p[i]到p[j+1]的最短路必过p[j]的最后一个j ans.push_back(p[i=j++]);//p[j]要停一次,不然的话可能下一次最短路不过p[j] } cout<<ans.size()<<endl; for (vector<int>::iterator it=ans.begin();it!=ans.end();it++)cout<<*it<<' ';//输出 }