#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105, MAXM = 1000005, INF = (1 << 29);
int N, M, adj[MAXN][MAXN], a[MAXM], sz;
char c[MAXN][MAXN];
vector<int> ans;
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> c[i][j];
      adj[i][j] = c[i][j] - '0';
      if (!adj[i][j]) adj[i][j] = INF;
    }
    adj[i][i] = 0;
  }
  cin >> M;
  for (int i = 0; i < M; i++) cin >> a[i];
  for (int k = 1; k <= N; k++)
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++)
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
  ans.push_back(0);
  for (int i = 1; i < M; i++)
    if (adj[a[ans[sz]]][a[i]] < i - ans[sz]) ans.push_back(i - 1), sz++;
  cout << ans.size() + 1 << "\n";
  for (auto i : ans) cout << a[i] << " ";
  cout << a[M - 1] << "\n";
  return 0;
}
