#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int g[n + 1][n + 1];
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= n; j++) g[i][j] = s[j - 1] - '0';
  }
  long long dist[n + 1][n + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        dist[i][j] = 0;
      else if (g[i][j] == 1)
        dist[i][j] = 1;
      else
        dist[i][j] = INT_MAX;
    }
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (dist[i][j] > dist[i][k] + dist[k][j])
          dist[i][j] = dist[i][k] + dist[k][j];
  int m;
  cin >> m;
  int p[m];
  for (int i = 0; i < m; i++) cin >> p[i];
  vector<int> ans;
  ans.push_back(p[0]);
  for (int i = 2; i < m; i++)
    if (dist[ans[ans.size() - 1]][p[i]] <
        dist[ans[ans.size() - 1]][p[i - 1]] + dist[p[i - 1]][p[i]])
      ans.push_back(p[i - 1]);
  ans.push_back(p[m - 1]);
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
  return 0;
}
