#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long mod = 1e9;
const int phash = 3;
const long long modhash = 1000000000000000003;
int dist[105][105];
int n;
void calc() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) dist[i][j] = 1e9;
    dist[i][i] = 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      if (c == '1') dist[i][j] = 1;
    }
  calc();
  int m;
  cin >> m;
  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<pair<int, int> > ans;
  ans.push_back({a[0], 0});
  for (int i = 2; i < m; i++) {
    int d = dist[ans.back().first][a[i]];
    int dl = i - ans.back().second;
    if (d < dl) ans.push_back({a[i - 1], i - 1});
  }
  ans.push_back({a[m - 1], 0});
  cout << ans.size() << '\n';
  for (auto i : ans) cout << i.first + 1 << ' ';
}
