#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class A, class B, class C>
struct Triple {
  A first;
  B second;
  C third;
  bool operator<(const Triple& t) const {
    if (first != t.first) return first < first.st;
    if (second != t.second) return second < t.second;
    return third < t.third;
  }
};
long long gcd(long long a, long long b) {
  while (a && b)
    if (a > b)
      a = a % b;
    else
      b = b % a;
  return a + b;
}
long long bp(long long a, long long b, long long& md) {
  long long res = 1;
  while (b) {
    if (b & 1) res *= a;
    a *= a;
    a %= md;
    res %= md;
    b >>= 1;
  }
  return res;
}
const long double EPS = 0.000000001;
const long double INFD32 = 1000000000;
const long long INF64 = 1000000000000000000;
const long long INF32 = 1000000000;
const long long one = 1;
long long G[1010][1010];
long long p[1000100];
long long dp[1000100];
long long fr[1000100];
long long len[1010][1010];
bool vis[1010];
long long n;
void BFS(long long v) {
  queue<long long> q;
  q.push(v);
  memset(vis, 0, sizeof(vis));
  vis[v] = 1;
  long long x = v;
  for (long long i = 1; i <= n; ++i) len[v][i] = INF32;
  len[v][v] = 0;
  while (q.size()) {
    v = q.front();
    q.pop();
    for (long long i = 1; i <= n; ++i) {
      if (G[v][i] && !vis[i]) {
        vis[i] = 1;
        len[x][i] = len[x][v] + 1;
        q.push(i);
      }
    }
  }
}
signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (long long j = 1; j <= n; ++j) G[i][j] = s[j - 1] - '0';
  }
  long long m;
  cin >> m;
  for (long long i = 1; i <= m; ++i) {
    cin >> p[i];
  }
  for (long long i = 1; i <= n; ++i) BFS(i);
  dp[1] = 1;
  for (long long i = 2; i <= m; ++i) {
    dp[i] = dp[i - 1] + 1;
    fr[i] = i - 1;
    for (long long j = 1; j <= n; ++j) {
      if (i - j < 1) break;
      long long v = i - j;
      long long LEN = len[p[v]][p[i]];
      if (dp[v] + 1 < dp[i] && j == LEN) {
        fr[i] = v;
        dp[i] = dp[v] + 1;
      }
    }
  }
  cout << dp[m] << "\n";
  fr[1] = 0;
  vector<long long> anss;
  while (m > 0) {
    anss.push_back(p[m]);
    m = fr[m];
  }
  reverse((anss).begin(), (anss).end());
  for (long long i : anss) cout << i << " ";
  return 0;
}
