#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
int n, m, g[103][103], path[103][103], dis[103][103], c[103][103], p[1000003],
    ret[1000003], cnt = 0;
vector<int> pa[103][103];
void getP(int i, int j, int x, int y) {
  int k = path[x][y];
  if (k == 0) return;
  pa[i][j].push_back(k);
  getP(i, j, x, k);
  getP(i, j, k, y);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int i, j, k, t, cur, idx;
  char ch;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      cin >> ch;
      g[i][j] = ch - '0';
      if (ch == '0')
        dis[i][j] = 0x3f3f3f3f;
      else
        dis[i][j] = 1;
    }
    dis[i][i] = 0;
  }
  for (k = 1; k <= n; k++) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        int tmp = dis[i][k] + dis[k][j];
        if (tmp < dis[i][j]) {
          dis[i][j] = tmp;
          c[i][j] = 1;
        } else if (tmp == dis[i][j]) {
          c[i][j]++;
        }
      }
    }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) getP(i, j, i, j);
  cin >> m;
  for (i = 0; i < m; i++) cin >> p[i];
  ret[cnt++] = p[0];
  idx = 0;
  for (i = 2; i < m; i++) {
    if (dis[p[idx]][p[i]] != i - idx) {
      ret[cnt++] = p[i - 1];
      idx = i - 1;
    }
  }
  ret[cnt++] = p[m - 1];
  cout << cnt << endl;
  for (i = 0; i < cnt; i++) cout << ret[i] << ' ';
  cout << endl;
  return 0;
}
