#include <bits/stdc++.h>
using namespace std;
int g[105][105] = {};
int dp[1000005], par[1000005];
vector<int> pm[105];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      g[i][j] = s[j] - '0';
      if (!g[i][j] && i != j) {
        g[i][j] = 10000;
      }
    }
  }
  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 m;
  cin >> m;
  vector<int> p(m);
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    p[i]--;
    pm[p[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    sort(pm[i].begin(), pm[i].end());
  }
  memset(dp, 0x3f, sizeof(dp));
  dp[m - 1] = 1;
  memset(par, -1, sizeof(par));
  for (int i = m - 2; i >= 0; i--) {
    for (int j = i + 1; j < m && j < (i + 200); j++) {
      if (g[p[i]][p[j]] == j - i) {
        if (dp[i] > 1 + dp[j]) {
          dp[i] = 1 + dp[j];
          par[i] = j;
        }
      }
    }
  }
  cout << dp[0] << endl;
  int r = 0, cnt = 0;
  while (r != -1) {
    cout << p[r] + 1 << " ";
    r = par[r];
    cnt++;
  }
  assert(cnt == dp[0]);
  cout << endl;
  return 0;
}
