#include <bits/stdc++.h>
using namespace std;
const int INF = 100000;
int d[101][101];
int main() {
  srand(clock());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; i++) cin >> s[i];
  int m;
  cin >> m;
  vector<int> a(m);
  for (int i = 0; i < m; i++) cin >> a[i], a[i]--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        if (s[i][j] == '1')
          d[i][j] = 1;
        else
          d[i][j] = INF;
      }
    }
  }
  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]);
  vector<int> ans;
  ans.push_back(a[0]);
  int last = 0;
  for (int i = 1; i < m; i++) {
    if (d[a[last]][a[i]] != i - last) {
      ans.push_back(a[i - 1]);
      last = i - 1;
    }
  }
  ans.push_back(a[m - 1]);
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] + 1 << ' ';
  }
  return 0;
}
