#include <bits/stdc++.h>
using namespace std;
int n, m;
long long mt[110][110];
vector<int> v;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  for (long long i = (0); i < (int)(n); ++i) {
    for (long long k = (0); k < (int)(n); ++k) {
      char c;
      cin >> c;
      if (c == '1')
        mt[i][k] = 1;
      else if (i == k)
        mt[i][k] = 0;
      else
        mt[i][k] = 2e9 + 5;
    }
  }
  for (long long i = (0); i < (int)(n); ++i) {
    for (long long j = (0); j < (int)(n); ++j) {
      for (long long k = (0); k < (int)(n); ++k) {
        mt[i][j] = min(mt[i][j], mt[i][k] + mt[k][j]);
      }
    }
  }
  cin >> m;
  v = vector<int>(m);
  for (long long i = (0); i < (int)(m); ++i) {
    cin >> v[i];
  }
  vector<int> ans;
  ans.push_back(v[0]);
  int d = 2;
  int ant = v[0] - 1;
  for (long long i = (2); i < (int)(m); ++i) {
    if (mt[ant][v[i] - 1] < d) {
      ans.push_back(v[i - 1]);
      d = 2;
      ant = v[i - 1] - 1;
    } else
      d++;
  }
  ans.push_back(v.back());
  cout << ans.size() << '\n';
  for (long long i = (0); i < (int)((int)ans.size()); ++i) {
    if (i != 0) cout << " ";
    cout << ans[i];
  }
  cout << '\n';
  return 0;
}
