#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
void bfs(int n, int s, vector<long long> adj[], vector<long long> sp[]) {
  vector<bool> vis(n, false);
  vis[s] = true;
  queue<int> q;
  q.push(s);
  vector<long long> d(n, -1);
  d[s] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : adj[v])
      if (vis[i] == false) vis[i] = true, q.push(i), d[i] = d[v] + 1;
  }
  for (int(i) = (0); i < n; i++) sp[s][i] = d[i];
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> adj[n];
  for (int(i) = (0); i < n; i++) {
    string s;
    cin >> s;
    for (int(j) = (0); j < n; j++)
      if (s[j] == '1') adj[i].push_back(j);
  }
  vector<long long> sp[n];
  for (int(i) = (0); i < n; i++) {
    for (int(j) = (0); j < n; j++) sp[i].push_back(0);
  }
  for (int(i) = (0); i < n; i++) bfs(n, i, adj, sp);
  long long m;
  cin >> m;
  vector<long long> p(m);
  for (int(i) = (0); i < (long long)(p.size()); i++) cin >> p[i];
  ;
  vector<pair<long long, long long> > seq;
  seq.push_back({p[0] - 1, 0});
  while (seq.back().second != m - 1) {
    long long j = seq.back().second + 1;
    pair<long long, long long> t = {seq.back().first, seq.back().second};
    while (j < m && sp[t.first][p[j] - 1] == j - t.second) j++;
    seq.push_back({p[j - 1] - 1, j - 1});
  }
  cout << (long long)(seq.size()) << "\n";
  for (int(i) = (0); i < (long long)(seq.size()); i++)
    cout << seq[i].first + 1 << " \n"[i == (long long)(seq.size()) - 1];
  return 0;
}
