#include <bits/stdc++.h>
using namespace std;
int n, m, a[1005000];
char e[105][105];
int h[105][105];
int dp[1005000], pre[1005000];
int ans[1005000], tot;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%s", e[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) e[i][j] = (e[i][j] == '0' ? 0 : 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (!e[i][j])
        h[i][j] = 1 << 26;
      else
        h[i][j] = 1;
  for (int i = 1; i <= n; i++) h[i][i] = 0;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) h[i][j] = min(h[i][j], h[i][k] + h[k][j]);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
  int p = 1;
  dp[1] = 1;
  for (int i = 2; i <= m; i++) {
    while (h[a[p]][a[i]] != i - p) p++;
    dp[i] = dp[p] + 1;
    pre[i] = p;
  }
  printf("%d\n", dp[m]);
  for (p = m; p; p = pre[p]) ans[++tot] = a[p];
  for (int i = tot; i; i--) printf("%d ", ans[i]);
  return 0;
}
