#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e6 + 5;
const long long maxn = 1e6 + 5;
const long long mo_num = 1e9 + 7;
long long n, m, a[maxn], b[maxn], ans;
long long g[105][105];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    string k;
    cin >> k;
    for (long long j = 0; j <= k.length() - 1; j++) {
      if (k[j] == '1') {
        g[i][j + 1] = 1;
      } else {
        g[i][j + 1] = INF;
      }
    }
    g[i][i] = 0;
  }
  for (long long k = 1; k <= n; k++) {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  cin >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> a[i];
  }
  long long cnt = 0;
  long long dis = 0;
  for (long long i = 1; i <= m; i++) {
    if (i == 1) {
      b[++cnt] = a[i];
      continue;
    } else {
      dis += g[a[i - 1]][a[i]];
      if (dis > g[b[cnt]][a[i]]) {
        b[++cnt] = a[i - 1];
        dis = g[a[i - 1]][a[i]];
      }
    }
  }
  b[++cnt] = a[m];
  cout << (cnt) << '\n';
  for (long long i = 1; i <= cnt; i++) cout << b[i] << " ";
  return 0;
}
