#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFLL = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 5;
int n, m;
int sp[101][101], p[MAXN];
int main() {
  scanf("%d", &n);
  for (long long i = 0; i < n; i++) {
    string aux;
    cin >> aux;
    for (long long j = 0; j < n; j++) {
      sp[i][j] = aux[j] - '0';
      if (i != j && !sp[i][j]) sp[i][j] = 2000;
    }
  }
  for (long long k = 0; k < n; k++) {
    for (long long i = 0; i < n; i++) {
      if (i != k && sp[i][k] < 2000)
        for (long long j = 0; j < n; j++) {
          sp[i][j] = min(sp[i][j], sp[i][k] + sp[k][j]);
        }
    }
  }
  scanf("%d", &m);
  for (long long i = 0; i < m; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }
  vector<int> ans;
  ans.push_back(p[0]);
  int x = 1;
  for (long long i = 0; i < m; i++) {
    while (i + x < m && x == sp[p[i]][p[i + x]]) {
      x++;
    }
    x--;
    ans.push_back(p[i + x]);
    i = i + x - 1;
    if (i == m - 2) break;
  }
  int k = ans.size();
  printf("%d\n", k);
  for (long long i = 0; i < k; i++) {
    printf("%d", ans[i] + 1);
    if (i == k - 1)
      printf("\n");
    else
      printf(" ");
  }
  return 0;
}
