#include <bits/stdc++.h>
int dx[] = {0, -1, 0, 1, -1, 1, -1, 1};
int dy[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int N = 1e6 + 5, oo = 1e9;
const long long MOD = 1e9 + 7, MOD2 = 1e9 + 9, p1 = 1000003, p2 = 1000033;
const double pi = acos(-1);
using namespace std;
vector<int> g[105];
int arr[N], floyd[105][105], n;
bool vis[N];
string adj[105];
void Floyd() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        floyd[i][j] = min(floyd[i][k] + floyd[k][j], floyd[i][j]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> adj[i];
    for (int j = 0; j < (int)(adj[i]).size(); j++) {
      if (adj[i][j] == '1') {
        g[i].push_back(j + 1);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < n; j++) floyd[i][j + 1] = adj[i][j] == '1' ? 1 : oo;
  Floyd();
  for (int i = 1; i <= n; i++) floyd[i][i] = 0;
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> arr[i];
  vector<int> vec;
  vec.push_back(arr[1]);
  int dist = 0;
  for (int i = 2; i <= m; i++) {
    dist++;
    if (floyd[vec.back()][arr[i]] < dist) {
      dist = 1;
      vec.push_back(arr[i - 1]);
    }
  }
  vec.push_back(arr[m]);
  cout << (int)(vec).size() << '\n';
  for (auto it : vec) cout << it << ' ';
  return 0;
}
