#include <bits/stdc++.h>
using namespace std;
int main() {
  long n;
  cin >> n;
  long **a = new long *[n];
  for (long i = 0; i < n; i++) a[i] = new long[n];
  for (long i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (long j = 0; j < s.length(); j++) a[i][j] = s[j] - '0';
  }
  for (long k = 0; k < n; k++)
    for (long i = 0; i < n; i++)
      for (long j = 0; j < n; j++) {
        if (a[i][k] == 0 || a[k][j] == 0)
          continue;
        else if (a[i][k] + a[k][j] < a[i][j] || a[i][j] == 0)
          a[i][j] = a[i][k] + a[k][j];
      }
  long m;
  cin >> m;
  long *path = new long[m];
  for (long i = 0; i < m; i++) cin >> path[i];
  long prev = path[0] - 1;
  vector<long> final;
  final.push_back(path[0] - 1);
  for (long i = 1; i < m - 1; i++) {
    long next = path[i + 1] - 1;
    long curr = path[i] - 1;
    if (a[prev][curr] + a[curr][next] != a[prev][next] || prev == next) {
      final.push_back(curr);
      prev = curr;
    }
  }
  final.push_back(path[m - 1] - 1);
  cout << final.size() << endl;
  for (long i = 0; i < final.size(); i++) cout << final[i] + 1 << " ";
  cout << endl;
}
