#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n;
  int matrix[n][n];
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      matrix[i][j] = s[j] - 48;
      if (!matrix[i][j]) matrix[i][j] = 1e4;
    }
  }
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (matrix[i][k] + matrix[k][j] < matrix[i][j])
          matrix[i][j] = matrix[i][k] + matrix[k][j];
  cin >> m;
  int seq[m];
  for (int i = 0; i < m; i++) {
    cin >> seq[i];
    seq[i]--;
  }
  for (int i = 0; i < n; i++) matrix[i][i] = 0;
  ans.push_back(seq[0]);
  bool found = true;
  int init = 0;
  while (found) {
    int j = init + 1;
    found = false;
    while (j < m &&
           matrix[seq[init]][seq[j]] ==
               (matrix[seq[init]][seq[j - 1]] + matrix[seq[j - 1]][seq[j]])) {
      found = true;
      j++;
    }
    if (found) ans.push_back(seq[j - 1]);
    init = j - 1;
  }
  cout << ans.size() << endl;
  for (int i : ans) cout << (i + 1) << " ";
  cout << endl;
}
