#include <bits/stdc++.h>
using namespace std;
int g[100 + 5][100 + 5], cost[100 + 5][100 + 5], v[1000000 + 5];
vector<int> way;
void umin(int &a, int b) { a = min(a, b); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s;
  int i, j, z;
  for (i = 0; i < n; i++) {
    cin >> s;
    for (j = 0; j < n; j++) g[i][j] = s[j] - '0';
  }
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (i == j)
        cost[i][j] = 0;
      else if (g[i][j] == 1)
        cost[i][j] = 1;
      else
        cost[i][j] = (INT_MAX / 2 - 1);
  for (z = 0; z < n; z++)
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        if (i != j) umin(cost[i][j], cost[i][z] + cost[z][j]);
  int m;
  cin >> m;
  for (i = 0; i < m; i++) cin >> v[i], v[i]--;
  way.push_back(v[0]);
  int nod;
  for (i = 1; i < m - 1; i++) {
    nod = way[(int)(way).size() - 1];
    if (cost[nod][v[i + 1]] < cost[nod][v[i]] + cost[v[i]][v[i + 1]])
      way.push_back(v[i]);
  }
  way.push_back(v[m - 1]);
  cout << (int)(way).size() << '\n';
  for (int x : way) cout << x + 1 << ' ';
  return 0;
}
