#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void MIN(T &a, T b) {
  if (a > b) a = b;
}
template <typename T>
inline void MAX(T &a, T b) {
  if (a < b) a = b;
}
const int N = 105;
vector<int> g[N], ans;
int d[N][N], n, m, p[1000005], prv[N][N][N];
void setup() {
  cin >> n;
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (n); ++j) {
      char c;
      cin >> c;
      if (c == '1') g[i].push_back(j);
    }
  cin >> m;
  for (int i = (1); i <= (m); ++i) cin >> p[i];
}
void ijk(int d[], int s) {
  for (int i = (1); i <= (n); ++i) d[i] = 1e9;
  d[s] = 0;
  priority_queue<pair<int, int> > q;
  q.push(make_pair(0, s));
  while (!q.empty()) {
    int l = -q.top().first;
    int u = q.top().second;
    q.pop();
    if (l > d[u]) continue;
    for (int v : g[u]) {
      if (d[v] > l + 1) {
        d[v] = l + 1;
        q.push(make_pair(-d[v], v));
      }
    }
  }
}
void pre() {
  for (int i = (1); i <= (n); ++i) {
    ijk(d[i], i);
    for (int j = (1); j <= (n); ++j) {
      for (int v : g[j]) {
        if (d[i][j] + 1 == d[i][v]) {
          prv[i][j][v] = 1;
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  setup();
  pre();
  ans.push_back(p[1]);
  for (int i = (3); i <= (m); ++i) {
    int ver = ans.back();
    if (prv[ver][p[i - 1]][p[i]] == 0) ans.push_back(p[i - 1]);
  }
  ans.push_back(p[m]);
  cout << ans.size() << '\n';
  for (int v : ans) cout << v << ' ';
  return 0;
}
