#include <bits/stdc++.h>
using namespace std;
int ans(string s) {}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int> > graph(n, vector<int>(n, 0));
  vector<vector<int> > dist(n, vector<int>(n, 0));
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      graph[i][j] = (s[j] - '0');
    }
  }
  int m;
  cin >> m;
  vector<int> p(m);
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++) {
    vector<int> visited(n, false);
    queue<int> qu;
    qu.push(i);
    visited[i] = true;
    dist[i][i] = 0;
    while (!qu.empty()) {
      int x = qu.front();
      qu.pop();
      for (int j = 0; j < n; j++) {
        if (graph[x][j] == 1 && !visited[j]) {
          visited[j] = true;
          dist[i][j] = dist[i][x] + 1;
          qu.push(j);
        }
      }
    }
  }
  int ct = 1;
  int i = 0;
  vector<int> ans;
  ans.push_back(p[0]);
  while (i < m - 1) {
    int j = i;
    ct++;
    while (i < m - 1 && dist[p[j]][p[i + 1]] == (i + 1 - j)) i++;
    ans.push_back(p[i]);
  }
  cout << ct << '\n';
  for (int l = 0; l < ct; l++) cout << ans[l] + 1 << ' ';
  cout << '\n';
}
