#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
const int inf = 0x3f3f3f3f;
int n, mm;
char s[150][150];
int m[150][150];
int dis[150][150];
int a[N];
int flag[N];
int main() {
  scanf("%d", &n);
  ;
  for (int i = (1); i < (n + 1); i++) scanf("%s", s[i]);
  for (int i = (1); i < (n + 1); i++) {
    for (int j = (1); j < (n + 1); j++) {
      if (i == j)
        dis[i][j] = 0;
      else {
        if (s[i][j - 1] == '0')
          dis[i][j] = inf;
        else
          dis[i][j] = 1;
      }
    }
  }
  scanf("%d", &mm);
  ;
  for (int i = (1); i < (mm + 1); i++) {
    scanf("%d", &a[i]);
    ;
    flag[i] = 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][k] + dis[k][j], dis[i][j]);
      }
  int pos = 1;
  int vis = 0;
  int ans = mm;
  while (pos < mm) {
    for (int j = pos + 1; j <= pos + n && j <= mm; j++) {
      if (dis[a[pos]][a[j]] == j - pos) vis = j;
    }
    for (int j = pos + 1; j < vis; j++) {
      flag[j] = 0;
      ans--;
    }
    pos = vis;
  }
  printf("%d\n", ans);
  for (int i = (1); i < (mm + 1); i++) {
    if (flag[i]) printf("%d ", a[i]);
  }
}
