#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
const int maxn = 3000005;
int a[105][105], book[105];
int b[maxn], dp[maxn], pre[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char x;
      scanf(" %c", &x);
      a[i][j] = x - '0';
      if (!a[i][j] && i != j) a[i][j] = 0x3f3f3f3f;
    }
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= m; i++) {
    dp[i] = dp[i - 1] + 1;
    pre[i] = i - 1;
    for (int j = 1; j <= n; j++)
      if (b[i] != j && a[j][b[i]] != 0x3f3f3f3f && book[j] && i >= a[j][b[i]] &&
          i - a[j][b[i]] == book[j]) {
        if (dp[i] > dp[book[j]] + 1) {
          dp[i] = dp[book[j]] + 1;
          pre[i] = book[j];
        }
      }
    book[b[i]] = i;
  }
  int now = m;
  vector<int> ans;
  while (now) {
    ans.push_back(b[now]);
    now = pre[now];
  }
  cout << dp[m] << endl;
  for (int i = ans.size() - 1; i >= 0; i--) printf("%d ", ans[i]);
  return 0;
}
