#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n;
  int dist[n][n];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      if (c == '1')
        dist[i][j] = 1;
      else
        dist[i][j] = 1e9;
    }
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  cin >> m;
  int arr[m], ans[m], cnt = m, last = 0, next = 1;
  for (int i = 0; i < m; i++) cin >> arr[i], arr[i]--, ans[i] = arr[i];
  while (next < m - 1)
    if (dist[arr[last]][arr[next + 1]] >= next + 1 - last &&
        arr[last] != arr[next + 1])
      ans[next] = -1, next++, cnt--;
    else
      last = next, next++;
  cout << cnt << '\n';
  for (int x : ans)
    if (x != -1) cout << x + 1 << ' ';
  cout << '\n';
  return 0;
}
