#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int64_t n;
  cin >> n;
  char mat[n + 1][n + 1];
  for (int64_t i = 1; i <= n; i++) {
    for (int64_t j = 1; j <= n; j++) cin >> mat[i][j];
  }
  int64_t m;
  cin >> m;
  int64_t p[m];
  for (int64_t i = 0; i < m; i++) cin >> p[i];
  int64_t dp[n + 1][n + 1];
  for (int64_t i = 1; i <= n; i++) {
    for (int64_t j = 1; j <= n; j++) {
      dp[i][j] = (int)(mat[i][j] - '0');
      if (dp[i][j] == 0 && i != j) dp[i][j] = INT_MAX;
    }
  }
  for (int64_t k = 1; k <= n; k++) {
    for (int64_t i = 1; i <= n; i++) {
      for (int64_t j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  vector<pair<int64_t, int64_t>> ans;
  ans.push_back({p[0], 0});
  for (int64_t i = 1; i < m; i++) {
    if (dp[ans.back().first][p[i]] < (i - ans.back().second)) {
      ans.push_back({p[i - 1], i - 1});
    }
  }
  ans.push_back({p[m - 1], m - 1});
  cout << ans.size() << '\n';
  for (int64_t i = 0; i < ans.size(); i++) cout << ans[i].first << ' ';
  return 0;
}
