#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int inf = 10000;
char graph[maxn][maxn];
vector<int> ans;
int n, a[1000010], m, dis[maxn][maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%s", graph[i] + 1);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i == j)
        dis[i][j] = 0;
      else if (graph[i][j] == '1')
        dis[i][j] = 1;
      else
        dis[i][j] = inf;
    }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  ans.push_back(a[1]);
  int tmp = 1;
  for (int i = 2; i <= m; i++) {
    if (i - tmp > dis[a[tmp]][a[i]]) {
      ans.push_back(a[i - 1]);
      tmp = i - 1;
    }
  }
  ans.push_back(a[m]);
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
  return 0;
}
