#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> adj[101];
pair<int, int> stored[101][101];
int main() {
  cin >> n;
  int adjm[n][n];
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      adjm[i][j] = (s[j] == '1');
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) continue;
      if (adjm[i][j] == 1) {
        adj[i].push_back(j);
      }
    }
  }
  for (int source = 0; source < n; source++) {
    bool vis[n];
    memset(vis, 0, sizeof vis);
    queue<pair<int, int> > topr;
    for (int i = 0; i < n; i++) {
      stored[source][i].first = INT_MAX;
    }
    topr.push(make_pair(source, -1));
    while (!topr.empty()) {
      pair<int, int> nxt = topr.front();
      topr.pop();
      int node = nxt.first;
      int last = nxt.second;
      vis[node] = true;
      int givendist = 0;
      if (last != -1) givendist = stored[source][last].first + 1;
      int currdist = stored[source][node].first;
      if (givendist < currdist) {
        stored[source][node].first = givendist;
        stored[source][node].second = last;
        for (int i = 0; i < adj[node].size(); i++) {
          int other = adj[node][i];
          if (!vis[other]) {
            vis[other] = true;
            topr.push(make_pair(other, node));
          }
        }
      }
    }
  }
  int ps;
  cin >> ps;
  int per[ps];
  for (int i = 0; i < ps; i++) {
    cin >> per[i];
    per[i]--;
  }
  vector<int> taken;
  taken.push_back(per[0]);
  taken.push_back(per[1]);
  int position = 2;
  while (position < ps) {
    int last = taken[taken.size() - 1];
    int sec = taken[taken.size() - 2];
    int thi = per[position];
    position++;
    if (stored[sec][last].first + 1 == stored[sec][thi].first) {
      taken.pop_back();
      taken.push_back(thi);
    } else {
      taken.push_back(thi);
    }
  }
  cout << taken.size() << endl;
  for (int i = 0; i < taken.size(); i++) {
    cout << taken[i] + 1 << ' ';
  }
}
