#include <bits/stdc++.h>
using namespace std;
vector<int> adjList[105];
int d[105][105];
int n, m;
int x;
queue<int> Q;
int color[105];
void bfs(int r) {
  d[r][r] = 0;
  Q.push(r);
  color[r] = r;
  while (Q.size()) {
    int u = Q.front();
    Q.pop();
    for (int v : adjList[u]) {
      if (color[v] != r) {
        color[v] = r;
        d[r][v] = d[r][u] + 1;
        Q.push(v);
      }
    }
  }
}
vector<int> subsequence;
vector<int> path;
int main() {
  cin >> n;
  string s;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') adjList[i].push_back(j + 1);
    }
  }
  for (int u = 1; u <= n; u++) {
    bfs(u);
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> x;
    path.push_back(x);
  }
  subsequence.push_back(path[0]);
  int last = 0;
  int i = 1;
  while (i < m) {
    if (d[path[last]][path[i]] < (i - last)) {
      last = i - 1;
      subsequence.push_back(path[i - 1]);
    }
    i++;
  }
  subsequence.push_back(path[m - 1]);
  cout << subsequence.size() << '\n';
  for (int v : subsequence) {
    cout << v << " ";
  }
  return 0;
}
