#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 10;
const int maxm = 1e6 + 10;
int n, m;
char mp[maxn][maxn];
int dp[maxn][maxn];
int vv[maxm], ans[maxm];
int main() {
  scanf("%d", &n);
  getchar();
  for (int i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
    for (int j = 1; j <= n; j++) {
      if (mp[i][j] == '1') {
        dp[i][j] = 1;
      } else {
        dp[i][j] = 0x3f3f3f3f;
      }
      if (i == j) {
        dp[i][j] = 0;
      }
    }
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &vv[i]);
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  int cnt = 0, dis = 0;
  ans[++cnt] = vv[1];
  for (int i = 2; i <= m; i++) {
    dis += dp[vv[i - 1]][vv[i]];
    if (dis > dp[ans[cnt]][vv[i]]) {
      ans[++cnt] = vv[i - 1];
      dis = dp[ans[cnt]][vv[i]];
    }
  }
  ans[++cnt] = vv[m];
  cout << cnt << endl;
  for (int i = 1; i <= cnt; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
