#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int maxm = 5000 + 5;
const int MOD = 1e9 + 7;
int mp[110][110];
int n, m;
int p[maxn];
vector<int> ans;
void Floyd(int len) {
  for (int k = 0; k < len; k++)
    for (int i = 0; i < len; i++)
      for (int j = 0; j < len; j++)
        mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = (0); i <= (n - 1); i++) {
    for (int j = (0); j <= (n - 1); j++) {
      char c;
      cin >> c;
      if (c == '0')
        mp[i][j] = 0x3f3f3f3f;
      else
        mp[i][j] = 1;
    }
    mp[i][i] = 0;
  }
  Floyd(n);
  cin >> m;
  for (int i = (1); i <= (m); i++) cin >> p[i], p[i]--;
  ans.push_back(p[1] + 1);
  for (int i = (2); i <= (m - 1); i++) {
    int pp = ans.back() - 1;
    if (mp[pp][p[i]] + mp[p[i]][p[i + 1]] <= mp[pp][p[i + 1]])
      continue;
    else
      ans.push_back(p[i] + 1);
  }
  ans.push_back(p[m] + 1);
  int t = ans.size();
  cout << t << endl;
  for (int i = (0); i <= (t - 1); i++) cout << ans[i] << " ";
  return 0;
}
