#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e2 + 7;
int g[MAXN][MAXN];
int n;
void processDist() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
}
int getDist(int a, int b) { return g[a][b]; }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        g[i][j] = 1;
      } else {
        g[i][j] = MAXN;
      }
      if (i == j) g[i][i] = 0;
    }
  }
  processDist();
  int m;
  cin >> m;
  vector<int> p(m);
  for (auto &x : p) {
    cin >> x;
    x--;
  }
  int pos = 0;
  vector<int> ans;
  ans.push_back(p[pos]);
  while (pos < m - 1) {
    int cur = 0;
    while (pos + cur + 1 < m && getDist(p[pos], p[pos + cur + 1]) == cur + 1)
      cur++;
    ans.push_back(p[pos + cur]);
    pos += cur;
  }
  if (ans.back() != p.back()) ans.push_back(p.back());
  cout << ans.size() << "\n";
  for (auto x : ans) {
    cout << x + 1 << " ";
  }
  return 0;
}
