#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, unsigned long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
  }
  return res;
}
long long int powermod(long long int x, unsigned long long int y,
                       long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  string s[n + 1];
  for (long long int i = (long long int)(0); i < (long long int)(n); i++)
    cin >> s[i];
  long long int d[n + 1][n + 1];
  for (long long int i = (long long int)(0); i < (long long int)(n); i++)
    for (long long int j = (long long int)(0); j < (long long int)(n); j++)
      d[i][j] = (s[i][j] == '1' ? 1 : 1e18);
  for (long long int i = (long long int)(0); i < (long long int)(n); i++)
    d[i][i] = 0;
  for (long long int k = 0; k < n; ++k) {
    for (long long int i = 0; i < n; ++i) {
      for (long long int j = 0; j < n; ++j) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  long long int m;
  cin >> m;
  vector<long long int> p;
  for (long long int i = (long long int)(0); i < (long long int)(m); i++) {
    long long int t;
    cin >> t;
    p.push_back(t);
  }
  vector<long long int> ans;
  ans.push_back(p[0]);
  long long int prev = 0;
  for (long long int i = (long long int)(1); i < (long long int)(p.size());
       i++) {
    if (d[ans.back() - 1][p[i] - 1] < (i - prev))
      ans.push_back(p[i - 1]), prev = i - 1;
  }
  ans.push_back(p.back());
  cout << ans.size() << "\n";
  for (const auto itr : ans) {
    cout << itr << ' ';
  }
  cout << "\n";
  ;
  return 0;
}
