#include <bits/stdc++.h>
#pragma GCC optimze("Ofast")
using namespace std;
const long long infl = 0x3f3f3f3f3f3f3f3fLL;
const long long infi = 0x3f3f3f3f;
const long long mod = 1000000007;
long long powm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
void solve() {
  long long n;
  cin >> n;
  vector<vector<long long> > dis(n + 1, vector<long long>(n + 1, infl));
  for (long long i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (long long j = 1; j <= n; j++) {
      if (s[j - 1] == '1') dis[i][j] = 1;
      if (i == j) dis[i][j] = 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++) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  long long l;
  cin >> l;
  vector<long long> path(l);
  for (long long i = 0; i <= l - 1; i++) cin >> path[i];
  vector<long long> ans;
  ans.push_back(path[0]);
  long long prev = path[0], previd = 0;
  for (long long i = 2; i <= l - 1; i++) {
    if (dis[prev][path[i]] < i - previd) {
      ans.push_back(path[i - 1]);
      previd = i - 1;
      prev = path[i - 1];
    }
  }
  ans.push_back(path.back());
  cout << ans.size() << '\n';
  for (long long v : ans) cout << v << " ";
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(12);
  long long t = 1;
  while (t--) solve();
  return 0;
}
