#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline int read() {
  int r = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    r = r * 10 + ch - '0';
    ch = getchar();
  }
  return r * f;
}
void print(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) print(x / 10);
  putchar(x % 10 + '0');
}
long long e[101][101];
string s[101];
int f[1000010];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  for (int i = (0); i < (n); ++i) {
    cin >> s[i];
  }
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j < (n); ++j) {
      e[i + 1][j + 1] = (s[i][j] == '0' ? 2147483647 : 1);
    }
  }
  for (int i = (1); i < (n + 1); ++i) e[i][i] = 0;
  int i, j, k;
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        if (e[i][j] > e[i][k] + e[k][j]) e[i][j] = e[i][k] + e[k][j];
  int m;
  cin >> m;
  for (int i = (0); i < (m); ++i) {
    cin >> f[i];
  }
  int now = f[0], t = 0;
  for (int i = (1); i < (m - 1); ++i) {
    if (e[now][f[i]] + e[f[i]][f[i + 1]] <= e[now][f[i + 1]]) {
      f[i] = -1;
      ++t;
    } else {
      now = f[i];
    }
  }
  cout << m - t << "\n";
  for (int i = (0); i < (m); ++i) {
    if (f[i] != -1) {
      cout << f[i] << ' ';
    }
  }
  return 0;
}
