#include <bits/stdc++.h>
using namespace std;
bool adj[101][101];
int dist[101][101];
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      if (c == '1') adj[i][j] = true;
      if (i == j)
        dist[i][i] = 0;
      else if (c == '1')
        dist[i][j] = 1;
      else
        dist[i][j] = 99999;
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (dist[i][j] > dist[i][k] + dist[k][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
  int m;
  cin >> m;
  vector<int> p(m);
  for (int i = 0; i < m; i++) {
    cin >> p[i];
  }
  int probe = 1, head = 0;
  vector<int> res;
  res.push_back(p[0]);
  while (probe < m) {
    if (dist[p[head] - 1][p[probe] - 1] == probe - head)
      probe++;
    else {
      head = probe - 1;
      res.push_back(p[head]);
    }
  }
  if (probe == m) res.push_back(p[m - 1]);
  cout << res.size() << endl;
  for (int i = 0; i < res.size(); i++) {
    cout << res[i] << " ";
  }
  return 0;
}
