#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<char> > board(n, vector<char>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
  vector<vector<int> > dist(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    queue<int> d;
    d.push(i);
    vector<bool> used(n, false);
    used[i] = true;
    while (!d.empty()) {
      int peak = d.front();
      d.pop();
      for (int j = 0; j < n; j++) {
        if (!used[j] && board[peak][j] == '1') {
          d.push(j);
          used[j] = true;
          dist[i][j] = dist[i][peak] + 1;
        }
      }
    }
  }
  int m;
  cin >> m;
  vector<int> p(m);
  for (int i = 0; i < m; i++) {
    cin >> p[i];
  }
  vector<int> ans;
  int left = 0;
  int right = 1;
  ans.push_back(p[0]);
  while (right < m) {
    if (right - left > dist[p[left] - 1][p[right] - 1]) {
      left = right - 1;
      ans.push_back(p[right - 1]);
    }
    right++;
  }
  ans.push_back(p[m - 1]);
  int k = ans.size();
  cout << k << '\n';
  for (int i = 0; i < k; i++) {
    cout << ans[i] << ' ';
  }
  return 0;
}
