#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
long long n, m, dist[110][110], adj[110][110];
vector<long long> v;
signed main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (long long j = 1; j <= n; j++)
      adj[i][j] = s[j - 1] - '0',
      dist[i][j] = (adj[i][j] == 1 ? 1 : (i == j ? 0 : INF));
  }
  for (long long k = 1; k <= n; k++)
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= n; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  cin >> m;
  long long last, pivot;
  cin >> last;
  v.push_back(last);
  pivot = last;
  for (long long i = 1; i < m; i++) {
    long long x;
    cin >> x;
    if (i == m - 1) {
      if (dist[pivot][x] < dist[pivot][last] + dist[last][x]) v.push_back(last);
      v.push_back(x);
      continue;
    }
    if (pivot == x) {
      v.push_back(last);
      swap(last, pivot);
      continue;
    }
    if (dist[pivot][x] < dist[pivot][last] + dist[last][x])
      v.push_back(last), pivot = last;
    last = x;
  }
  cout << v.size() << "\n";
  for (auto u : v) cout << u << " ";
}
