#include <bits/stdc++.h>
using namespace std;
using P = pair<long, int>;
int n;
long d[110][110];
void warshall_floyd() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1')
        d[i][j] = 1;
      else if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = 1000000;
    }
  }
  int m;
  cin >> m;
  int p[m];
  for (auto &pi : p) cin >> pi;
  vector<int> ans;
  ans.push_back(p[0]);
  int cur = 0, next = 1;
  while (next + 1 < m) {
    int i = p[cur] - 1, k = p[next] - 1, j = p[next + 1] - 1;
    if (d[i][k] + d[k][j] > d[i][j]) {
      ans.push_back(k + 1);
      cur = next;
    }
    next++;
  }
  cout << ans.size() + 1 << '\n';
  for (auto ai : ans) cout << ai << ' ';
  cout << p[m - 1] << '\n';
  return 0;
}
