#include <bits/stdc++.h>
using namespace std;
int path[int(2e6) + 1];
bool matrix[101][101];
int dis[101][101];
void floyd(int n) {
  memset(dis, 0x3f3f3f3f, 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 (matrix[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++)
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
void solve(int n, int m) {
  int sp[int(2e6) + 1];
  int cnt = 0;
  sp[cnt] = path[0];
  int cur = sp[cnt];
  int len = 0;
  for (int i = 1; i < m; i++) {
    if (dis[sp[cnt]][path[i]] != len + 1) {
      len = 1;
      ++cnt;
      sp[cnt] = cur;
      cur = path[i];
    } else {
      cur = path[i];
      ++len;
    }
  }
  ++cnt;
  sp[cnt] = cur;
  cout << (cnt + 1) << endl;
  for (int i = 0; i <= cnt; i++) cout << sp[i] << " ";
}
int main() {
  int n, m;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= s.length(); j++) matrix[i][j] = (s[j - 1] == '1');
  }
  cin >> m;
  for (int i = 0; i < m; i++) cin >> path[i];
  floyd(n);
  solve(n, m);
  return 0;
}
