#include <bits/stdc++.h>
using namespace std;
const int maxn = 105, maxm = 1000005;
const int inf = 0x3f3f3f3f;
int f[maxn][maxn];
int p[maxm], d[maxm], pre[maxm], nxt[maxm];
int main() {
  int n, m;
  char ch;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> ch;
      f[i][j] = ch - '0';
      if (f[i][j] == 0) f[i][j] = inf;
    }
  }
  for (int i = 1; i <= n; ++i) f[i][i] = 0;
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        f[i][j] = min(f[i][j], min(inf, f[i][k] + f[k][j]));
      }
    }
  }
  cin >> m;
  nxt[0] = 1, pre[m + 1] = m;
  for (int i = 1; i <= m; ++i) {
    cin >> p[i];
    d[i] = 1;
    pre[i] = i - 1;
    nxt[i] = i + 1;
  }
  int cnt = 0;
  for (int i = 2; i < m; ++i) {
    if (d[pre[i]] + d[i] == f[p[pre[i]]][p[nxt[i]]]) {
      d[pre[i]] += d[i];
      nxt[pre[i]] = nxt[i];
      pre[nxt[i]] = pre[i];
      ++cnt;
    }
  }
  cout << m - cnt << endl;
  for (int i = nxt[0]; i != m + 1; i = nxt[i]) {
    cout << p[i] << ' ';
  }
  cout << endl;
  return 0;
}
