#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  char x[n][n + 1];
  int v[n][n];
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    for (int j = 0; j < n; j++) {
      if (x[i][j] == '0')
        v[i][j] = 1e9;
      else
        v[i][j] = 1;
      if (i == j) v[i][j] = 0;
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) v[i][j] = min(v[i][j], v[i][k] + v[k][j]);
    }
  }
  int M, m = 0;
  cin >> M;
  int P[M], p[M];
  for (int i = 0; i < M; i++) {
    cin >> P[i];
    P[i]--;
  }
  for (int i = 0; i < M; i++)
    if (i == 0 || P[i] != P[i - 1]) {
      p[m++] = P[i];
    }
  vector<int> V;
  for (int i = 1; i < m;) {
    V.push_back(p[i - 1]);
    int l = i - 1;
    while (i < m) {
      if (v[p[l]][p[i]] != i - l) break;
      i++;
    }
  }
  V.push_back(p[m - 1]);
  printf("%d\n", V.size());
  for (int i = 0; i < (int)V.size(); i++) {
    printf("%d ", V[i] + 1);
  }
  puts("");
}
