#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<vector<char>> g(n, vector<char>(n));
  vector<vector<int>> md(n, vector<int>(n, 1e9));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> g[i][j];
      if (g[i][j] == '1') {
        md[i][j] = 1;
      } else {
        md[i][j] = 1e9;
      }
    }
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        md[i][j] = min(md[i][j], md[i][k] + md[k][j]);
      }
    }
  }
  int m;
  cin >> m;
  vector<int> a(m);
  vector<int> dp(m, 1e9);
  vector<int> pr(m, -1);
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
    a[i]--;
    if (i == 0) {
      dp[0] = 1;
    }
    for (int j = i - 1; j >= max(0, i - n); --j) {
      if (i - j == md[a[j]][a[i]] && a[i] != a[j] && dp[i] > dp[j] + 1) {
        dp[i] = dp[j] + 1;
        pr[i] = j;
      }
    }
  }
  cout << dp[m - 1] << '\n';
  int c = m - 1;
  vector<int> ans;
  while (c != -1) {
    ans.push_back(a[c]);
    c = pr[c];
  }
  for (int i = ans.size() - 1; i >= 0; --i) cout << ans[i] + 1 << ' ';
}
