#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<string> v(n + 1);
  vector<vector<long long>> dis(n + 1, vector<long long>(n + 1, mod));
  for (long long i = 1; i <= n; i++) cin >> v[i];
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < n; j++) {
      if (v[i][j] == '1') dis[i][j + 1] = 1;
    }
  }
  for (long long i = 1; i <= n; i++) dis[i][i] = 0;
  for (long long k = 1; k <= n; k++) {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  long long m;
  cin >> m;
  vector<long long> a(m + 1);
  for (long long i = 1; i <= m; i++) cin >> a[i];
  long long cur = 1;
  vector<long long> ans;
  ans.push_back(a[cur]);
  for (long long i = 2; i <= m; i++) {
    if (dis[a[cur]][a[i]] < i - cur) {
      cur = i - 1;
      ans.push_back(a[cur]);
    }
  }
  ans.push_back(a[m]);
  cout << ans.size() << "\n";
  for (auto i : ans) cout << i << " ";
}
