#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  string adj[n];
  for (int i = 0; i < n; i++) cin >> adj[i];
  long long m;
  cin >> m;
  long long a[m];
  for (int i = 0; i < m; i++) cin >> a[i];
  long long dist[n + 1][n + 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i == j)
        dist[i][j] = 0;
      else if (adj[i - 1][j - 1] == '1')
        dist[i][j] = 1;
      else
        dist[i][j] = 1e9;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  vector<long long> res;
  res.push_back(a[0]);
  int now = 0;
  for (int i = 1; i < m; i++) {
    now += dist[a[i - 1]][a[i]];
    if (dist[res.back()][a[i]] != now) {
      if (res.back() != a[i - 1]) res.push_back(a[i - 1]);
      now = dist[a[i - 1]][a[i]];
    }
  }
  if (res.back() != a[m - 1]) res.push_back(a[m - 1]);
  cout << res.size() << "\n";
  for (auto it : res) cout << it << " ";
  cout << "\n";
  return 0;
}
