#include <bits/stdc++.h>
using namespace std;
int n;
int m;
vector<vector<int> > g;
vector<int> p;
vector<int> ans;
long long INF = 1e9;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  g.resize(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '1')
        g[i].push_back(1);
      else
        g[i].push_back(INF);
    }
    g[i][i] = 0;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    p.push_back(x - 1);
  }
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (g[i][k] < INF && g[k][j] < INF)
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
  ans.push_back(p[0]);
  int posl = 0;
  int posr = 0;
  while (posl < m - 1 && posr < m) {
    int v1 = p[posl];
    int v2 = p[posr];
    while (posr < m && g[v1][v2] == posr - posl) {
      posr++;
      if (posr < m) v2 = p[posr];
    }
    if (posr == m) {
      ans.push_back(p[m - 1]);
      posl = m;
    } else {
      ans.push_back(p[posr - 1]);
      posl = posr - 1;
    }
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 << ' ';
}
