#include <bits/stdc++.h>
using namespace std;
char mp[110][110];
int d[110][110];
int p[1000010];
int pre[1000010];
int dp[1000010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d", &p[i]);
  memset(d, 0x3f, sizeof(d));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (mp[i][j] == '1') d[i][j] = mp[i][j] - '0';
  for (int i = 1; i <= n; i++) d[i][i] = 0;
  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][j], d[i][k] + d[k][j]);
  memset(dp, 0x3f, sizeof(dp));
  dp[1] = 1;
  for (int i = 2; i <= m; i++) {
    int D = max(1, i - n);
    for (int j = i - 1; j >= D; j--) {
      if (d[p[j]][p[i]] == (i - j)) {
        if (dp[j] + 1 < dp[i]) {
          dp[i] = dp[j] + 1;
          pre[i] = j;
        }
      }
    }
  }
  printf("%d\n", dp[m]);
  vector<int> v;
  int now = m;
  while (now) {
    v.push_back(p[now]);
    now = pre[now];
  }
  for (int i = v.size() - 1; i >= 0; i--) printf("%d ", v[i]);
  return 0;
}
