#include <bits/stdc++.h>
using namespace std;
vector<string> g;
vector<vector<int>> d;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  g.resize(n);
  d.resize(n);
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    d[i].resize(n, 10000000);
    for (int j = 0; j < n; ++j) {
      if (s[j] == '1') {
        d[i][j] = 1;
      }
    }
  }
  for (int k = 0; k < 2; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int c = 0; c < n; ++c) {
          d[i][j] = min(d[i][j], d[i][c] + d[c][j]);
        }
      }
    }
  }
  int m;
  cin >> m;
  vector<int> numbers(m);
  for (int i = 0; i < m; ++i) {
    cin >> numbers[i];
  }
  int last_pos = 0;
  int cur_pos = 1;
  vector<int> answer;
  answer.push_back(numbers[0]);
  while (cur_pos != m - 1) {
    if (numbers[last_pos] != numbers[cur_pos + 1] &&
        d[numbers[last_pos] - 1][numbers[cur_pos + 1] - 1] ==
            d[numbers[cur_pos] - 1][numbers[cur_pos + 1] - 1] +
                d[numbers[last_pos] - 1][numbers[cur_pos] - 1]) {
      ++cur_pos;
    } else {
      answer.push_back(numbers[cur_pos]);
      last_pos = cur_pos;
      ++cur_pos;
    }
  }
  answer.push_back(numbers[m - 1]);
  cout << answer.size() << endl;
  for (int i = 0; i < (int)answer.size(); ++i) {
    cout << answer[i] << " ";
  }
  cout << endl;
  return 0;
}
