#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int adj[n + 1][n + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      if (c == '0') {
        adj[i][j] = 1e5;
      } else {
        adj[i][j] = 1;
      }
      if (i == j) {
        adj[i][j] = 0;
      }
    }
  }
  int m;
  cin >> m;
  int p[m];
  for (int i = 0; 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++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }
  vector<int> ans;
  ans.push_back(p[0]);
  int cur = p[0];
  int dist = 0;
  for (int i = 1; i < m - 1; i++) {
    dist++;
    if (adj[cur][p[i + 1]] < dist + 1) {
      cur = p[i];
      ans.push_back(p[i]);
      dist = 0;
    }
  }
  ans.push_back(p[m - 1]);
  cout << ((int)(ans).size()) << endl;
  for (auto x : ans) {
    cout << x << " ";
  }
  return 0;
}
