#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << '=' << h << endl;
  _dbg(sdbg + 1, a...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  for (auto vv : V) os << vv << " ";
  return os;
}
void solve() {
  long long n, k, i, j;
  cin >> n;
  vector<vector<long long> > g(n, vector<long long>(n));
  vector<vector<long long> > d(n, vector<long long>(n));
  for (long long i = (0); i <= ((long long)(n)-1); i++)
    for (long long j = (0); j <= ((long long)(n)-1); j++) {
      char t;
      cin >> t;
      if (t == '0') {
        g[i][j] = 0;
        d[i][j] = 3000;
      } else {
        g[i][j] = 1;
        d[i][j] = 1;
      }
    }
  cin >> k;
  vector<long long> p(k), pm;
  for (long long i = (0); i <= ((long long)(k)-1); i++) cin >> p[i];
  for (long long k = 0; k < n; k++) {
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        if (i == j) d[i][j] = 0;
      }
    }
  }
  pm.push_back(0);
  for (long long i = 1; i < k; i++) {
    if (d[p[pm.back()] - 1][p[i] - 1] < i - pm.back()) pm.push_back(i - 1);
  }
  pm.push_back(k - 1);
  cout << pm.size() << endl;
  for (long long i = (0); i <= ((long long)(pm.size()) - 1); i++)
    cout << p[pm[i]] << " ";
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
