#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;
int t, id, n, m, x, y, k, c, xx, yy, ans;
vector<int> v, adj[N];
bool fl, ok;
long long res, sum, tot;
char ch;
char s[N], z[N];
int mat[101][101], arr[N], f[101], out[101];
void floyd() {
  for (int k = 0; k < n; ++k)
    for (int u = 0; u < n; ++u)
      for (int v = 0; v < n; ++v)
        mat[u][v] = min(mat[u][v], mat[u][k] + mat[k][v]);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    for (int j = 0; s[j]; ++j)
      mat[i][j] = s[j] == '1' ? 1 : 0x3f3f3f3f * (i != j);
  }
  floyd();
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d", arr + i);
    --arr[i];
  }
  v.push_back(arr[0]);
  int pre = arr[0];
  for (int i = 1; i < m - 1; ++i) {
    int cur = arr[i], nxt = arr[i + 1];
    if (mat[pre][cur] + mat[cur][nxt] != mat[pre][nxt]) {
      v.push_back(cur);
      pre = cur;
    }
  }
  v.push_back(arr[m - 1]);
  printf("%d", (int)v.size());
  ch = '\n';
  for (int i : v) {
    printf("%c%d", ch, i + 1);
    ch = ' ';
  }
  return 0;
}
