#include <bits/stdc++.h>
using namespace std;
const int INF = 1000100010;
int n, m;
string g[101];
int p[1000001];
int d[101][101];
vector<int> ans;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = (0); i < (n); ++i) cin >> g[i];
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (n); ++j) {
      if (g[i][j] == '1')
        d[i][j] = 1;
      else if (i != j)
        d[i][j] = INF;
    }
  cin >> m;
  for (int i = (0); i < (m); ++i) cin >> p[i], --p[i];
  for (int k = (0); k < (n); ++k)
    for (int i = (0); i < (n); ++i)
      for (int j = (0); j < (n); ++j) {
        if (d[i][k] == INF || d[k][j] == INF) continue;
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
  int pre = p[0], cur = 0;
  ans.emplace_back(p[0]);
  for (int i = (2); i < (m); ++i) {
    if (d[pre][p[i - 1]] + d[p[i - 1]][p[i]] != d[pre][p[i]])
      ans.emplace_back(p[i - 1]), pre = p[i - 1];
  }
  ans.emplace_back(p[m - 1]);
  cout << ans.size() << endl;
  for (auto it : ans) cout << it + 1 << " ";
  cout << endl;
  return 0;
}
