#include <bits/stdc++.h>
using namespace std;
const int N = 111, M = 1011111, iNF = 0x7ffffff;
int n, m, path[M], gr[N][N], dis[N][N];
vector<int> res;
int main() {
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dis[i][j] = gr[i][j] = (getchar() == '1' ? 1 : iNF);
    getchar();
  }
  cin >> m;
  for (int i = 1; i <= m; i++) scanf("%d", path + i);
  for (int i = 1; i <= n; i++) dis[i][i] = 0;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (i != j && j != k && i != k)
          dis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]);
  int last = path[1];
  res.push_back(last);
  for (int i = 2; i < m; i++) {
    if (dis[last][path[i + 1]] <
        dis[last][path[i]] + dis[path[i]][path[i + 1]]) {
      last = path[i];
      res.push_back(last);
    }
  }
  res.push_back(path[m]);
  cout << res.size() << endl;
  for (int i = 0; i < res.size(); i++) cout << res[i] << " ";
  cout << endl;
  return 0;
}
