#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const int M = 1e6 + 6, INF = 1e9 + 6;
int n;
int m;
int p[M];
int a[N][N];
int d[N][N];
void pre() {
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (d[i][k] < INF && d[k][j] < INF)
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.precision(17);
  cin >> n;
  string s[n];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) d[i][j] = INF;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < n; j++) {
      a[i][j] = s[i][j] - '0';
      if (a[i][j] != 0) d[i][j] = 1;
    }
  }
  for (int i = 0; i < n; i++) d[i][i] = 0;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    p[i]--;
  }
  pre();
  vector<int> ans;
  int so = p[0], si = 0;
  ans.push_back(so);
  for (int i = 1; i < m; i++) {
    if (d[so][p[i]] != i - si) {
      ans.push_back(p[i - 1]);
      so = p[i - 1];
      si = i - 1;
    }
  }
  ans.push_back(p[m - 1]);
  cout << ans.size() << endl;
  for (auto x : ans) cout << x + 1 << " ";
  cout << endl;
  return 0;
}
