#include <bits/stdc++.h>
using namespace std;
const int maxn = 110, maxm = 1 * 1000 * 1000 + 10;
int n, m, u, dis[maxn][maxn], p[maxm];
queue<int> q;
char s[maxn];
vector<int> ans, vec[maxn];
bool mark[maxn];
void bfs(int x) {
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (auto w : vec[u]) {
      if (!mark[w]) {
        dis[x][w] = dis[x][u] + 1;
        mark[w] = 1;
        q.push(w);
      }
    }
  }
}
int main() {
  memset(dis, 31, sizeof dis);
  ios::sync_with_stdio(0);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') vec[i].push_back(j);
    }
  }
  for (int i = 0; i < n; i++) {
    memset(mark, 0, sizeof mark);
    dis[i][i] = 0;
    mark[i] = 1;
    q.push(i);
    bfs(i);
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }
  ans.push_back(p[0]);
  u = 0;
  for (int i = 1; i < m; i++) {
    if (dis[ans.back()][p[i]] < i - u) {
      ans.push_back(p[i - 1]);
      u = i - 1;
    }
  }
  ans.push_back(p[m - 1]);
  printf("%d\n", (((int)ans.size())));
  for (auto w : ans) printf("%d ", w + 1);
  return 0;
}
