#include <bits/stdc++.h>
using namespace std;
int a[111][111], n, m, p[1111111];
int f[1111111], q[1111111];
void print(int i) {
  if (i == 1) {
    printf("%d", p[i] + 1);
    return;
  }
  print(q[i]);
  printf(" %d", p[i] + 1);
}
int main() {
  scanf("%d", &n);
  memset(a, 0x3f, sizeof a);
  for (int i = 0; i < n; ++i) {
    char s[111];
    scanf("%s\n", s);
    for (int j = 0; j < n; ++j) {
      if (s[j] == '1') a[i][j] = 1;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        a[j][k] = min(a[j][k], a[j][i] + a[i][k]);
      }
    }
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d", p + i);
    --p[i];
  }
  memset(f, 0x3f, sizeof f);
  memset(q, -1, sizeof q);
  f[1] = 1;
  q[1] = 1;
  for (int i = 2; i <= m; ++i) {
    int x = p[i], y;
    for (int j = max(1, i - n); j < i; ++j) {
      y = p[j];
      if (a[y][x] == i - j) {
        if (f[j] + 1 < f[i] && p[j] != p[i]) {
          f[i] = f[j] + 1;
          q[i] = j;
        }
      }
    }
  }
  printf("%d\n", f[m]);
  print(m);
  puts("");
  return 0;
}
