#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline void fre() {}
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch <= '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
int mp[110][110];
vector<int> ans;
int a[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    char s[110];
    scanf("%s", s + 1);
    for (int j = 1; j <= n; j++) {
      if (s[j] == '1') {
        mp[i][j] = 1;
      } else
        mp[i][j] = inf;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
      }
    }
  }
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) scanf("%d", &a[i]);
  int temp, last = 0, cnt = 1;
  for (int i = 1; i < m - 1; i++) {
    int len = i + 1 - last;
    if (a[last] != a[i + 1] && mp[a[last]][a[i + 1]] == len)
      a[i] = -1;
    else {
      last = i;
      cnt++;
    }
  }
  printf("%d\n", cnt + 1);
  for (int i = 0; i < m; i++) {
    if (a[i] != -1) {
      printf("%d ", a[i]);
    }
  }
  puts("");
}
