#include <bits/stdc++.h>
using namespace std;
const int INF = 11111111;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  while (cin >> n) {
    vector<vector<char>> adj(n, vector<char>(n));
    vector<vector<int>> dis(n, vector<int>(n));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> adj[i][j];
        if (i == j)
          dis[i][j] = 0;
        else if (adj[i][j] == '1')
          dis[i][j] = 1;
        else
          dis[i][j] = INF;
      }
    }
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
      }
    }
    int m;
    cin >> m;
    vector<int> p(m);
    for (int i = 0; i < m; ++i) cin >> p[i];
    vector<int> res;
    res.push_back(p[0]);
    for (int i = 1, j = 0; i < m; ++i) {
      if (dis[p[j] - 1][p[i] - 1] == i - j) continue;
      res.push_back(p[i - 1]);
      j = i - 1;
    }
    res.push_back(p[m - 1]);
    cout << res.size() << '\n';
    for (int i : res) cout << i << ' ';
    cout << '\n';
  }
  return 0;
}
