#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, o = 0;
  cin >> n;
  int adj[n + 1][n + 1];
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < n; ++j) {
      if (s[j] == '1') {
        adj[i][j + 1] = 1;
        ++o;
      } else
        adj[i][j + 1] = 1e5;
    }
  }
  int l;
  cin >> l;
  bool hai[101][l];
  memset(hai, false, sizeof(hai));
  int path[l];
  for (int i = 0; i < l; ++i) {
    cin >> path[i];
    hai[path[i]][i] = true;
  }
  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
  for (int k = 1; k <= n; ++k) {
    adj[k][k] = 0;
  }
  vector<int> ans;
  int val = n - 1;
  ans.push_back(path[0]);
  int j = 0;
  while (j != l - 1) {
    int nextj = -1;
    for (int i = 1; i <= n; ++i) {
      if (i == path[j]) continue;
      if (adj[path[j]][i] + j < l && hai[i][adj[path[j]][i] + j])
        nextj = max(nextj, adj[path[j]][i] + j);
    }
    j = nextj;
    ans.push_back(path[j]);
  }
  cout << (int)(ans).size() << '\n';
  for (int i = 0; i < (int)(ans).size(); ++i) cout << ans[i] << " ";
  cout << '\n';
  return 0;
}
