#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long exp(long long a, long long b, long long m) {
  if (b == 0) return 1;
  if (b == 1) return mod(a, m);
  long long k = mod(exp(a, b / 2, m), m);
  if (b & 1) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
long long dist[110][110];
string s[110];
int32_t main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (s[i][j] == '0') {
        dist[i + 1][j + 1] = 1e9;
      } else
        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++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  for (long long i = 1; i <= n; i++) dist[i][i] = 0;
  long long m;
  cin >> m;
  vector<long long> p(m);
  for (long long i = 0; i < m; i++) {
    cin >> p[i];
  }
  vector<long long> v;
  v.push_back(p[0]);
  queue<long long> last;
  for (long long i = 1; i < m - 1; i++) {
    long long x = v[max((long long)v.size() - 1, 0LL)];
    if (dist[x][p[i]] + dist[p[i]][p[i + 1]] == dist[x][p[i + 1]]) {
    } else
      v.push_back(p[i]);
  }
  v.push_back(p[m - 1]);
  cout << v.size() << "\n";
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << "\n";
}
