#include <bits/stdc++.h>
using namespace std;
int dist[101][101];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dist[i][j] = 1e9;
      if (j == i) {
        dist[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      if (s[i][j] == '1') {
        dist[i][j + 1] = 1;
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  int m;
  cin >> m;
  int arr[m];
  for (int i = 0; i < m; i++) {
    cin >> arr[i];
  }
  vector<int> ans;
  for (int i = 0; i < m; i++) {
    int maxi = i;
    for (int j = i + 1; j <= min(m - 1, i + n); j++) {
      if (dist[arr[i]][arr[j]] == j - i) {
        maxi = max(maxi, j);
      }
    }
    ans.push_back(arr[i]);
    if (i != maxi) i = maxi - 1;
  }
  cout << ans.size() << "\n";
  for (int j : ans) {
    cout << j << " ";
  }
}
