#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  unsigned int n;
  cin >> n;
  vector<unsigned int> adj(n * n);
  for (unsigned int i = 0; i < n; i++) {
    for (unsigned int j = 0; j < n; j++) {
      char c;
      cin >> c;
      adj[i * n + j] = c == '1' ? 1 : n;
    }
  }
  for (unsigned int i = 0; i < n; i++) {
    adj[i * n + i] = 0;
  }
  for (unsigned int k = 0; k < n; k++) {
    for (unsigned int i = 0; i < n; i++) {
      for (unsigned int j = 0; j < n; j++) {
        adj[i * n + j] = min(adj[i * n + j], adj[i * n + k] + adj[k * n + j]);
      }
    }
  }
  unsigned int m;
  cin >> m;
  unsigned int p2, p1, d = 1;
  cin >> p2 >> p1;
  p2--;
  p1--;
  vector<unsigned int> ans;
  ans.push_back(p2 + 1);
  for (unsigned int i = 2; i < m; i++) {
    unsigned int p;
    cin >> p;
    p--;
    if (adj[p2 * n + p] == d + 1) {
      p1 = p;
      d++;
    } else {
      p2 = p1;
      p1 = p;
      d = 1;
      ans.push_back(p2 + 1);
    }
  }
  ans.push_back(p1 + 1);
  cout << ans.size() << '\n' << ans[0];
  for (unsigned int i = 1; i < ans.size(); i++) {
    cout << ' ' << ans[i];
  }
  cout << endl;
  return 0;
}
