#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int max(long long int a, long long int b) {
  if (a > b)
    return a;
  else
    return b;
}
long long int min(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
long long int binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long binpow(long long a, long long b) {
  if (b == 0) return 1;
  long long res = binpow(a, b / 2);
  if (b % 2)
    return res * res * a;
  else
    return res * res;
}
void solve() {
  int n;
  cin >> n;
  vector<vector<bool> > adj(n, vector<bool>(n, 0));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') adj[i][j] = 1;
    }
  }
  vector<vector<int> > d(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    vector<bool> visit(n);
    visit[i] = true;
    queue<int> q;
    d[i][i] = 0;
    q.push(i);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int c = 0; c < n; c++) {
        if (adj[u][c]) {
          if (visit[c]) continue;
          visit[c] = true;
          d[i][c] = d[i][u] + 1;
          q.push(c);
        }
      }
    }
  }
  int m;
  cin >> m;
  vector<int> p(m);
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> dp(m, 1e9);
  vector<int> par(m);
  dp[0] = 1;
  par[0] = -1;
  for (int i = 1; i < m; i++) {
    vector<bool> check(n, 0);
    for (int j = i - 1; j >= 0; j--) {
      if (check[p[j]]) break;
      check[p[j]] = true;
      int dist = i - j;
      if (dist == d[p[j]][p[i]]) {
        dp[i] = min(dp[i], dp[j] + 1);
        par[i] = j;
      }
    }
  }
  cout << dp[m - 1] << endl;
  int curr = m - 1;
  vector<int> ans;
  while (curr != -1) {
    ans.push_back(p[curr]);
    curr = par[curr];
  }
  reverse(ans.begin(), ans.end());
  for (auto c : ans) {
    cout << c + 1 << " ";
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
