#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 20;
const int maxn = 110;
int n, m, G[maxn][maxn];
int ans[maxn * maxn * maxn], p[maxn * maxn * maxn], cnt;
void Floyd() {
  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      if (i != k)
        for (int j = 1; j <= n; ++j)
          if (i != j && j != k) G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
}
int main() {
  char c;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      cin >> c;
      if (c == '1')
        G[i][j] = 1;
      else
        G[i][j] = inf;
      if (i == j) G[i][j] = 0;
    }
  cin >> m;
  for (int i = 1; i <= m; ++i) cin >> p[i];
  Floyd();
  ans[++cnt] = p[1];
  int dis = 0;
  for (int i = 2; i <= m; ++i) {
    dis += G[p[i - 1]][p[i]];
    if (dis > G[ans[cnt]][p[i]]) {
      ans[++cnt] = p[i - 1];
      dis = G[ans[cnt]][p[i]];
    }
  }
  ans[++cnt] = p[m];
  cout << cnt << endl;
  for (int i = 1; i <= cnt; ++i) cout << ans[i] << ' ';
  cout << endl;
  return 0;
}
