#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<vector<int>> dist(n, vector<int>(n));
  constexpr int inf = 101 * 101;
  for (size_t i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (size_t j = 0; j < n; ++j) {
      dist[i][j] = (s[j] == '1') ? 1 : inf;
    }
  }
  for (size_t i = 0; i < n; ++i) {
    dist[i][i] = 0;
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (dist[i][k] + dist[k][j] < dist[i][j]) {
          dist[i][j] = dist[i][k] + dist[k][j];
        }
      }
    }
  }
  int m;
  cin >> m;
  vector<int> p(m);
  for (int i = 0; i < m; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<int> result;
  result.push_back(p[0]);
  for (int i = 1; i < m - 1; ++i) {
    if (dist[result.back()][p[i]] + dist[p[i]][p[i + 1]] !=
        dist[result.back()][p[i + 1]]) {
      result.push_back(p[i]);
    }
  }
  result.push_back(p.back());
  ostringstream oss;
  oss << result.size() << endl;
  for (const auto el : result) {
    oss << (el + 1) << " ";
  }
  oss << endl;
  cout << oss.str() << flush;
  return 0;
}
