#include <bits/stdc++.h>
using namespace std;
int D[200][200], n, m;
vector<int> S;
signed main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      char ch;
      cin >> ch;
      D[i][j] = ch - '0';
      if (i != j && ch == '0') D[i][j] = 1e7;
    }
  for (int k = 1; k <= n; k++)
    for (int u = 1; u <= n; u++)
      for (int v = 1; v <= n; v++) D[u][v] = min(D[u][v], D[u][k] + D[k][v]);
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    if (S.size() >= 2 &&
        D[S[S.size() - 2]][S.back()] + D[S.back()][x] == D[S[S.size() - 2]][x])
      S.pop_back();
    S.push_back(x);
  }
  cout << S.size() << "\n";
  for (auto i : S) cout << i << ' ';
}
