#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e6 + 10;
const int inf = 1e9 + 10;
const ll MOD = 998244353;
int n, a[101][101], d[101][101], m, p[N], last[101], dp[N];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      if (c == '1')
        d[i][j] = 1;
      else
        d[i][j] = inf;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> p[i];
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (j == p[i] || last[j] == 0) continue;
      if (i - last[j] == d[j][p[i]]) {
        dp[i] = max(dp[i], i - last[j]);
      }
    }
    last[p[i]] = i;
  }
  vector<int> v;
  int ind = m, cnt = 0;
  while (1) {
    v.push_back(p[ind]);
    if (ind == 1) break;
    ind -= dp[ind];
  }
  reverse(v.begin(), v.end());
  printf("%d\n", (int)v.size());
  for (int c : v) printf("%d ", c);
  return 0;
}
