#include <bits/stdc++.h>
using namespace std;
int n, m, a[1000010], ans[1000010], cnt = 0;
int edge[110][110];
char s[110];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= n; j++) {
      if (s[j] == '1')
        edge[i][j] = 1;
      else
        edge[i][j] = n + 1;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (edge[i][k] && edge[k][j] && i != j)
          edge[i][j] = min(edge[i][j], edge[i][k] + edge[k][j]);
      }
    }
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
  ans[++cnt] = a[1];
  for (int i = 2, last = a[1], tmp = edge[a[1]][a[2]]; i < m; i++) {
    if (tmp + edge[a[i]][a[i + 1]] != edge[last][a[i + 1]]) {
      tmp = edge[a[i]][a[i + 1]], last = a[i];
      ans[++cnt] = a[i];
    } else
      tmp += edge[a[i]][a[i + 1]];
  }
  ans[++cnt] = a[m];
  printf("%d\n", cnt);
  for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
  printf("\n");
}
