#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<long long> vc[10000], mc;
long long dist[1001][1001], graph[1000][1000], n, dp[1000001];
string s[1000];
long long p[1000001], m;
void floydWarshall() {
  long long i, j, k, V = n;
  for (i = 0; i < V; i++)
    for (j = 0; j < V; j++) dist[i][j] = graph[i][j];
  for (k = 0; k < V; k++) {
    for (i = 0; i < V; i++) {
      for (j = 0; j < V; j++) {
        if (dist[i][k] + dist[k][j] < dist[i][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
}
int32_t main() {
  fastio();
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> s[i];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (s[i][j] == '1') {
        if (s[i][j] == '1') {
          graph[i][j] = 1;
        }
        vc[i + 1].push_back(j + 1);
      } else
        graph[i][j] = INF;
    }
  }
  cin >> m;
  for (long long i = 0; i < m; i++) cin >> p[i];
  floydWarshall();
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (i == j) dist[i][j] = INF;
    }
  }
  long long bk = p[m - 1], xx = m - 1;
  mc.push_back(p[m - 1]);
  for (long long i = m - 2; i >= 0; i--) {
    if (dist[p[i] - 1][bk - 1] != (xx - i)) {
      mc.push_back(p[i + 1]);
      bk = p[i + 1];
      xx = i + 1;
      i++;
    }
  }
  mc.push_back(p[0]);
  cout << mc.size() << "\n";
  for (long long i = mc.size() - 1; i >= 0; i--) cout << mc[i] << " ";
}
