#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int MAXN = 1e6 + 105;
const int N = 5000 + 15;
const double EPS = 1e-9;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
int i, j, k, n, m;
char grid[105][105];
int p[MAXN], d[105][105];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> grid[i][j];
      d[i][j] = 1e9;
      if (grid[i][j] == '1') {
        d[i][j] = 1;
      }
      if (i == j) d[i][j] = 0;
    }
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> p[i];
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  vector<int> ansV(1, 1);
  for (int i = 2; i <= m; i++) {
    int is = d[p[ansV.back()]][p[i]];
    int haveToBe = i - ansV.back();
    if (is < haveToBe) {
      ansV.push_back(i - 1);
    }
  }
  ansV.push_back(m);
  cout << ansV.size() << endl;
  for (auto &e : ansV) cout << p[e] << ' ';
  return 0;
}
