#include <bits/stdc++.h>
using namespace std;
int n;
int graph[110][110];
int m;
int p[1000010];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        graph[i][j] = INT_MAX / 3;
      } else {
        graph[i][j] = 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '1') {
        graph[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        graph[j][k] = min(graph[j][k], graph[j][i] + graph[i][k]);
      }
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> ans;
  ans.push_back(p[0]);
  int i = 0;
  while (i < m - 1) {
    int cnt = 1;
    int j;
    for (j = i + 1; j < m; j++) {
      if (graph[p[i]][p[j]] != cnt) {
        break;
      }
      cnt++;
    }
    j--;
    ans.push_back(p[j]);
    i = j;
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] + 1;
    if (i < ans.size() - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
  return 0;
}
