#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1.0);
long long mod = 1e9 + 7;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
long long GCD(long long a, long long b) {
  if (b == 0) return a;
  return GCD(b, a % b);
}
long long prod(long long a, long long b) {
  long long res = 0;
  a %= mod;
  while (b) {
    if (b & 1ll) res = (res + a) % mod;
    a = (2ll * a) % mod;
    b >>= 1ll;
  }
  return res;
}
long long power(long long a, long long b) {
  long long res = 1ll;
  while (b > 0) {
    if (b % 2ll) res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2ll;
  }
  return res;
}
long long binaryExponentiation(long long a, long long b) {
  long long result = 1ll;
  while (b > 0) {
    if (b % 2 == 1) result = (result * a) % mod;
    a = (a * a) % mod;
    b /= 2ll;
  }
  return result;
}
long long ModularInverse(long long a) {
  return binaryExponentiation(a, mod - 2);
}
long long nx[4] = {1, -1, 0, 0};
long long ny[4] = {0, 0, 1, -1};
long long n, m;
vector<string> g(100);
vector<multiset<long long>> mp(101);
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> g[i];
  }
  cin >> m;
  vector<long long> p(m);
  ;
  for (long long i = 0; i < m; i++) {
    cin >> p[i];
  }
  vector<vector<long long>> dist(n + 1, vector<long long>(n + 1, m + 1));
  for (long long i = 0; i < n; i++) {
    dist[i + 1][i + 1] = 0;
    for (long long j = 0; j < n; j++) {
      if (g[i][j] == '0') continue;
      dist[i + 1][j + 1] = 1;
    }
  }
  for (long long k = 1; k <= n; k++) {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (dist[i][k] + dist[k][j] < dist[i][j]) {
          dist[i][j] = dist[i][k] + dist[k][j];
        }
      }
    }
  }
  vector<long long> d(m, m + 1);
  vector<long long> parent(m, -1);
  d[0] = 0;
  for (long long i = 0; i < m; i++) {
    if (d[i] > m) continue;
    for (long long j = 1; j <= n; j++) {
      long long x = dist[p[i]][j];
      if (i + x < m and p[i + x] == j) {
        if (d[i + x] > 1 + d[i]) {
          d[i + x] = 1 + d[i];
          parent[i + x] = i;
        }
      }
    }
  }
  vector<long long> ans;
  long long x = m - 1;
  while (x != -1) {
    ans.push_back(p[x]);
    x = parent[x];
  }
  cout << ans.size() << endl;
  for (auto it = ans.rbegin(); it != ans.rend(); it++) {
    cout << *it << " ";
  }
  cout << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
}
