#include <bits/stdc++.h>
#pragma GCC optimize("trapv")
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
mt19937 rng(
    (long long)std::chrono::steady_clock::now().time_since_epoch().count());
const long long MAXN = 3e5 + 10;
void solve(long long test_case) {
  long long n;
  cin >> n;
  long long dp[n + 1][n + 1];
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      char x;
      cin >> x;
      if (x == '1') {
        dp[i][j] = 1;
      } else if (i == j) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = 1e9;
      }
    }
  }
  for (long long k = 1; k <= n; k++) {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (dp[i][k] != 1e9 && dp[k][j] != 1e9) {
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
        }
      }
    }
  }
  long long m;
  cin >> m;
  long long p[m + 1];
  for (long long i = 1; i <= m; i++) cin >> p[i];
  vector<long long> ans = {p[1]};
  for (long long i = 1; i <= m; i++) {
    long long j = i + 1;
    while (j <= m && dp[p[i]][p[j]] == j - i) j++;
    j--;
    ans.push_back(p[j]);
    if (j < m)
      i = j - 1;
    else
      break;
  }
  cout << ans.size() << "\n";
  for (long long i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long T = 1;
  long long test_case = 1;
  while (T--) {
    solve(test_case);
    test_case++;
  }
}
