#include <bits/stdc++.h>
using namespace std;
int graph[105][105], path[1000005], n, m;
const int inf = 0x3f3f3f;
void floyd() {
  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        if (graph[i][j] > graph[i][k] + graph[k][j])
          graph[i][j] = graph[i][k] + graph[k][j];
}
int main() {
  ios::sync_with_stdio(false);
  int i, j;
  char x;
  cin >> n;
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      cin >> x;
      if (i == j)
        graph[i][j] = 0;
      else if (x == '1')
        graph[i][j] = 1;
      else
        graph[i][j] = inf;
    }
  }
  cin >> m;
  for (i = 1; i <= m; ++i) cin >> path[i];
  floyd();
  vector<int> list;
  list.push_back(path[1]);
  int pl = 1, pr = 1;
  while (pr <= m) {
    if (pr - pl == graph[path[pl]][path[pr]])
      ++pr;
    else {
      list.push_back(path[pr - 1]);
      pl = pr - 1;
    }
  }
  list.push_back(path[m]);
  cout << list.size() << endl;
  for (int x : list) cout << x << " ";
  return 0;
}
