#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int N = int(1e5) + 7;
vector<int> V[105];
vector<int> PATH;
int len[105][105];
bool del[1000005];
int n, p;
void init() {
  char c[105];
  memset(del, 0, sizeof del);
  for (int i = 0; i < 105; i++)
    for (int j = 0; j < 105; j++) len[i][j] = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", c);
    for (int j = 0; j < n; j++) {
      if (c[j] == '1') {
        V[i].push_back(j + 1);
      }
    }
  }
  scanf("%d", &p);
  for (int i = 0; i < p; i++) {
    int k;
    scanf("%d", &k);
    PATH.push_back(k);
  }
}
int short_path(int v, int u) {
  if (v == u) return 0;
  if (len[v][u] != 0) return len[v][u];
  vector<int> q;
  q.push_back(v);
  for (int i = 0; i < q.size(); i++) {
    int p = q[i];
    for (auto i : V[p]) {
      if (len[v][i] == 0 && i != v) {
        len[v][i] = len[v][p] + 1;
        q.push_back(i);
      }
    }
  }
  return len[v][u];
}
void solve() {
  init();
  int pre = 0;
  for (int i = 1; i < p - 1; i++) {
    if (short_path(PATH[pre], PATH[i]) + short_path(PATH[i], PATH[i + 1]) ==
        short_path(PATH[pre], PATH[i + 1]))
      del[i] = true;
    else
      pre = i;
  }
  queue<int> ans;
  for (int i = 0; i < p; i++)
    if (!del[i]) ans.push(PATH[i]);
  printf("%d\n", ans.size());
  int i = 0;
  while (!ans.empty()) {
    if (i > 0) printf(" ");
    printf("%d", ans.front());
    ans.pop();
    i++;
  }
  printf("\n");
}
int main() {
  int test_cases_number = 1;
  for (int i = 0; i < test_cases_number; i++) solve();
}
