#include <bits/stdc++.h>
using namespace std;
long long a[101][101];
long long dist[101][101];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < 101; i++) {
    for (long long j = 0; j < 101; j++) {
      if (i != j)
        dist[i][j] = 1e9;
      else
        dist[i][j] = 0;
    }
  }
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (long long j = 0; j < n; j++) {
      a[i][j] = s[j] - '0';
      if (a[i][j] == 1) {
        dist[i][j] = 1;
      }
    }
  }
  for (long long k = 0; k < n; k++) {
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        if (dist[i][k] + dist[k][j] < dist[i][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
  long long m;
  cin >> m;
  vector<long long> c(m);
  for (long long i = 0; i < m; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<long long> ans;
  ans.push_back(c[0]);
  for (int i = 0; i < m; i++) {
    if (i == m - 1) break;
    int j = i + 2;
    for (; j < m; j++) {
      if (dist[c[i]][c[j]] != j - i) {
        break;
      }
    }
    j--;
    i = j - 1;
    ans.push_back(c[j]);
  }
  cout << (int)ans.size() << endl;
  for (long long i = 0; i < ans.size(); i++) {
    cout << ans[i] + 1 << " ";
  }
}
