#include <bits/stdc++.h>
using namespace std;
vector<int> vec[1000], ans;
char ch[1000];
int ara[1000005], mn[105][105], ck[10000], m;
void bfs(int node) {
  deque<int> dq;
  ck[node] = node;
  mn[node][node] = 0;
  dq.push_back(node);
  int i, j, k;
  while (!dq.empty()) {
    j = dq.front();
    dq.pop_front();
    for (i = 0; i < vec[j].size(); i++) {
      k = vec[j][i];
      if (ck[k] == node) continue;
      ck[k] = node;
      dq.push_back(k);
      mn[node][k] = mn[node][j] + 1;
    }
  }
}
void op(int p) {
  int a, b, c, z, i, j;
  a = ara[p];
  for (i = p + 1; i <= m; i++) {
    if (i == m) {
      ans.push_back(ara[m]);
      return;
    }
    b = ara[i + 1];
    c = i - p + 1;
    if (mn[a][b] < c) {
      ans.push_back(ara[i]);
      op(i);
      return;
    }
  }
  return;
}
int main() {
  int n, i, j, k, a, b, c, x, y, z;
  cin >> n;
  for (i = 1; i <= n; i++) {
    scanf("%s", ch);
    for (j = 0; j < n; j++) {
      if (ch[j] == '1') {
        vec[i].push_back(j + 1);
      }
    }
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d", &ara[i]);
  }
  if (m > 1) {
    ans.push_back(ara[1]);
  }
  for (i = 1; i <= n; i++) {
    bfs(i);
  }
  op(1);
  z = ans.size();
  printf("%d\n", z);
  for (i = 0; i < z; i++) {
    printf("%d ", ans[i]);
  }
  printf("\n");
}
