#include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int M = 1001000;
int mat[N][N], can[N][N];
int a[M];
int main() {
  int n, m;
  char c;
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; j++) {
      cin >> c;
      if (c == '1')
        mat[i][j] = 1;
      else if (i != j)
        mat[i][j] = 10000000;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; ++k) {
        if (mat[j][i] < 10000000 && mat[i][k] < 10000000)
          mat[j][k] = min(mat[j][k], mat[j][i] + mat[i][k]);
      }
    }
  }
  cin >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
  }
  vector<int> rs;
  int i, j;
  i = 0, j = 1;
  rs.push_back(a[0]);
  while (j < m - 1) {
    if (mat[a[i]][a[j + 1]] < (mat[a[i]][a[j]] + mat[a[j]][a[j + 1]])) {
      rs.push_back(a[j]);
      i = j++;
    } else {
      j++;
    }
  }
  rs.push_back(a[m - 1]);
  cout << rs.size() << "\n";
  for (int i = 0; i < rs.size(); ++i) {
    cout << rs[i] << " ";
  }
  cout << "\n";
  return 0;
}
