#include <bits/stdc++.h>
using namespace std;
int dist[110][110], p[1000010], g[110][110], ans[1000010];
void floyd(int n) {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (dist[i][k] + dist[k][j] < dist[i][j])
          dist[i][j] = dist[i][k] + dist[k][j];
}
int main() {
  int n, ansnum = 0, last, m;
  string str;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> str;
    for (int j = 0; j < str.length(); j++) g[i][j + 1] = str[j] - 48;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i == j) dist[i][j] = 0;
      if (i != j)
        if (g[i][j] == 1)
          dist[i][j] = 1;
        else
          dist[i][j] = 1000;
    }
  floyd(n);
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> p[i];
  ansnum++;
  ans[1] = p[1];
  last = 1;
  for (int i = 2; i <= m; i++) {
    if (dist[p[last]][p[i]] < i - last) {
      ansnum++;
      ans[ansnum] = p[i - 1];
      last = i - 1;
    }
  }
  if (ans[ansnum] != m) {
    ansnum++;
    ans[ansnum] = p[m];
  }
  cout << ansnum << endl;
  for (int i = 1; i <= ansnum; i++) cout << ans[i] << " ";
  cout << endl;
}
