#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
char ch;
int n, m, dp[1000002], dist[102][102], p[1000002], last[1000002], ans[1000002];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> ch;
      dist[i][j] = ch - '0';
      if (!dist[i][j] && i != j) dist[i][j] = inf;
    }
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> p[i];
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (dist[i][k] + dist[k][j] < dist[i][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
  for (int i = 1; i <= m; i++) dp[i] = inf;
  dp[1] = 1;
  for (int i = 1; i <= m; i++) {
    if (dp[i] != inf)
      for (int j = 1; j <= n; j++)
        if (dist[p[i]][j] != inf)
          if (i + dist[p[i]][j] <= m && p[i + dist[p[i]][j]] == j) {
            if (dp[i + dist[p[i]][j]] > dp[i] + 1) {
              dp[i + dist[p[i]][j]] = dp[i] + 1;
              last[i + dist[p[i]][j]] = i;
            }
          }
  }
  cout << dp[m] << "\n";
  int nr = 0;
  int x = m;
  while (x) {
    ans[++nr] = p[x];
    x = last[x];
  }
  for (int i = nr; i >= 1; i--) cout << ans[i] << " ";
  return 0;
}
