#include <bits/stdc++.h>
using namespace std;
const int N = 110, M = 1000010, P = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline long long read();
inline void write(long long x);
inline void writeln(long long x);
int n, m;
int a[M];
char s[N][N];
int d[N][N];
int dis[N][N];
int ans[M];
inline long long read() {
  long long s = 0;
  bool flag = false;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') flag = true;
  for (; '0' <= ch && ch <= '9'; ch = getchar())
    s = (s << 3) + (s << 1) + (ch ^ 48);
  if (flag) return -s;
  return s;
}
inline void write(long long x) {
  if (!x) {
    putchar('0'), putchar(' ');
    return;
  }
  if (x < 0) putchar('-'), x = -x;
  char ch[20];
  int tot = 0;
  while (x) ch[++tot] = x % 10, x /= 10;
  for (int i = tot; i; i--) putchar(ch[i] + '0');
  putchar(' ');
}
inline void writeln(long long x) {
  write(x);
  putchar('\n');
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  memset(dis, inf, sizeof(dis));
  for (int i = 1; i <= n; i++) dis[i][i] = d[i][i] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (s[i][j] == '1') d[i][j] = dis[i][j] = 1;
  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]);
  m = read();
  for (int i = 1; i <= m; i++) a[i] = read();
  int now = 2, k = 1;
  ans[1] = a[1];
  for (int i = 1; i <= m; i = now - 1, now++) {
    while (now <= m && dis[a[i]][a[now]] >= now - i) now++;
    ans[++k] = a[now - 1];
  }
  k--;
  printf("%d\n", k);
  for (int i = 1; i <= k; i++) printf("%d ", ans[i]);
  putchar('\n');
  return 0;
}
