#include <bits/stdc++.h>
using namespace std;
const int maxn = 119;
vector<int> g[maxn];
int d[maxn][maxn];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char in;
      cin >> in;
      d[i][j] = in - '0';
      if (i != j && d[i][j] == 0) d[i][j] = 219;
      if (in == '1') g[i].push_back(j);
    }
  }
  int m;
  cin >> m;
  vector<int> p(m);
  for (int i = 0; i < m; i++) cin >> p[i];
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (d[i][k] + d[k][j] < d[i][j]) {
          d[i][j] = d[i][k] + d[k][j];
        }
      }
    }
  }
  vector<int> ap;
  ap.push_back(p[0] - 1);
  ap.push_back(p[1] - 1);
  int s = 1;
  for (int i = 2; i < m; i++) {
    int ss, sm, sl;
    ss = ap[s - 1];
    sm = ap[s];
    sl = p[i] - 1;
    if (d[ss][sm] + d[sm][sl] == d[ss][sl]) {
      ap.pop_back();
      ap.push_back(sl);
    } else {
      ap.push_back(sl);
      s++;
    }
  }
  cout << s + 1 << endl;
  for (int it : ap) {
    cout << it + 1 << " ";
  }
  cout << endl;
  return 0;
}
