#include <bits/stdc++.h>
using namespace std;
int n, m, d[105][105], p[1000005], ans[1000005], ck;
char a[105][105];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i]);
    for (int j = 0; j < n; j++) {
      if (a[i][j] == '1') {
        d[i][j + 1] = 1;
        ck++;
      } else
        d[i][j + 1] = 1e9;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
      }
    }
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &p[i]);
  }
  ans[1] = p[1];
  int ind = 2, dem = 2;
  while (ind <= m) {
    if (ind == 2)
      ans[ind] = p[2];
    else {
      int x = 1;
      if (d[ans[dem - 2]][p[ind]] ==
              d[ans[dem - 2]][ans[dem - 1]] + d[ans[dem - 1]][p[ind]] &&
          ans[dem - 2] != p[ind]) {
        ans[dem - 1] = p[ind];
        dem--;
      } else {
        ans[dem] = p[ind];
      }
    }
    ind++;
    dem++;
  }
  printf("%d\n", dem - 1);
  for (int i = 1; i < dem; i++) {
    printf("%d ", ans[i]);
  }
}
