#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  char a[n][n];
  int i, j, k;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  long long int p[n + 5][n + 5];
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (a[i][j] == '1') {
        p[i + 1][j + 1] = 1;
      } else {
        p[i + 1][j + 1] = 2000000000000000000LL;
      }
      if (i == j) {
        p[i + 1][j + 1] = 0;
      }
    }
  }
  for (k = 1; k <= n; k++) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        p[i][j] = min(p[i][j], p[i][k] + p[k][j]);
      }
    }
  }
  long long int path, mm = 0;
  cin >> path;
  long long int ps[path];
  long long int ans[path];
  for (i = 0; i < path; i++) {
    cin >> ps[i];
  }
  ans[mm] = ps[0];
  mm++;
  int ss = 1;
  for (i = 2; i < path; i++) {
    if (p[ans[mm - 1]][ps[i]] <
        p[ans[mm - 1]][ps[i - 1]] + p[ps[i - 1]][ps[i]]) {
      ans[mm] = ps[i - 1];
      mm++;
    }
  }
  ans[mm] = ps[path - 1];
  mm++;
  cout << mm << endl;
  for (i = 0; i < mm; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
