#include <bits/stdc++.h>
int graph[105][105];
int a[1000005];
int b[1000005];
int min(int a, int b) {
  if (a <= b) {
    return a;
  } else
    return b;
}
int main() {
  int m, k, n;
  scanf("%d", &n);
  getchar();
  memset(graph, 0x3f3f3f3f, sizeof(graph));
  int i, j, w;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      char temp;
      scanf("%c", &temp);
      if (temp == '1') {
        graph[i][j] = 1;
      }
    }
    getchar();
  }
  for (i = 1; i <= n; i++) {
    graph[i][i] = 0;
  }
  for (w = 1; w <= n; w++) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        graph[i][j] = min(graph[i][j], graph[i][w] + graph[w][j]);
      }
    }
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
  }
  b[1] = a[1];
  int start = 1, cnt = 2;
  for (i = 2; i <= m; i++) {
    if (graph[a[start]][a[i]] == i - start) {
      continue;
    } else if (graph[a[start]][a[i]] < i - start) {
      b[cnt] = a[i - 1];
      start = i - 1;
      cnt++;
    }
  }
  b[cnt] = a[m];
  printf("%d\n", cnt);
  for (i = 1; i <= cnt; i++) {
    printf("%d ", b[i]);
  }
}
