#include <bits/stdc++.h>
using namespace std;
const int TAM = 1e6 + 3;
int n, m;
string mat[105];
int path[TAM];
int dist[105][105];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(dist, -1, sizeof(dist));
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> mat[i];
    mat[i] = "-" + mat[i];
    for (int j = 1; j <= n; j++) {
      if (mat[i][j] == '1')
        dist[i][j] = 1;
      else if (i == j)
        dist[i][j] = 0;
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> path[i];
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        if (dist[i][k] == -1 or dist[k][j] == -1) continue;
        if (dist[i][j] == -1 or dist[i][j] > dist[i][k] + dist[k][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
  vector<int> ans;
  int last = -1;
  int v = -1;
  for (int i = 0; i < m; i++) {
    int curr = path[i];
    if (!i) {
      last = i;
      v = curr;
      ans.push_back(curr);
      continue;
    }
    if (dist[v][curr] == i - last) {
      if (i == m - 1) ans.push_back(curr);
    } else {
      ans.push_back(path[i - 1]);
      last = i - 1;
      v = path[i - 1];
      if (i == m - 1) ans.push_back(curr);
    }
  }
  cout << ans.size() << endl;
  for (int w : ans) {
    cout << w << " ";
  }
  cout << endl;
  return 0;
}
