#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, m, s;
int chara[102][102];
int dis[101][101];
int path[1000006];
vector<int> v;
void floyd() {
  memset(dis, 0x3f, sizeof(dis));
  for (int i = 1; i <= n; ++i) dis[i][i] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (chara[i][j]) dis[i][j] = 1;
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; ++j) {
        if (dis[i][j] > dis[i][k] + dis[k][j])
          dis[i][j] = dis[i][k] + dis[k][j];
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf("%1d", &chara[i][j]);
    }
  }
  floyd();
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d", path + i);
  }
  int ans = 1;
  v.push_back(path[m]);
  for (int i = m - 1, j = m; i >= 1; --i) {
    if (dis[path[i]][path[j]] < j - i) {
      ++ans;
      v.push_back(path[i + 1]);
      j = i + 1;
    }
    if (i == 1) v.push_back(path[i]), ++ans;
  }
  printf("%d\n", ans);
  for (int i = ans - 1; i >= 0; --i) printf("%d ", v[i]);
  printf("\n");
  return 0;
}
