#include <bits/stdc++.h>
void print_array(int *a, int n) {
  printf("#");
  for (int i = 0; i < n; i++) printf("%d%c", a[i], i == n - 1 ? '\n' : ' ');
}
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const long long INF = 0x3f3f3f3f3f3f3f3f;
inline long long readll() {
  long long x(0), op(1);
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') op = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * op;
}
const int sz = 1e6 + 9;
long long dis[200][200], n;
void calc() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
char s[200][200];
int a[sz];
void solve() {
  int m = readll();
  int cnt = 0;
  for (int i = 0; i < m; ++i) {
    a[cnt++] = readll();
    while (cnt >= 3 &&
           dis[a[cnt - 3]][a[cnt - 2]] + dis[a[cnt - 2]][a[cnt - 1]] ==
               dis[a[cnt - 3]][a[cnt - 1]]) {
      a[cnt - 2] = a[cnt - 1];
      cnt--;
    }
  }
  cout << cnt << endl;
  for (int i = 0; i < cnt; ++i) {
    cout << a[i] << ' ';
  }
  puts("");
}
int main() {
  cin >> n;
  for (int i = 1; i < n + 1; ++i) {
    scanf("%s", s[i] + 1);
  }
  for (int i = 1; i < n + 1; ++i) {
    for (int j = 1; j < n + 1; ++j) {
      if (i == j)
        dis[i][j] = 0;
      else if (s[i][j] == '1')
        dis[i][j] = 1;
      else
        dis[i][j] = 1e18;
    }
  }
  calc();
  solve();
  return 0;
}
