#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma comment(linker, "/STACK:256000000")
using namespace std;
static const int _ = []() {
  ios::sync_with_stdio(false);
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
template <typename T, typename U>
struct par {
  T x;
  U y;
  par() : x(0), y(0) {}
  par(const T& x, const U& y) : x(x), y(y) {}
  bool operator<(const par& other) const {
    if (x == other.x) return y < other.y;
    return x < other.x;
  }
  bool operator>(const par& other) const {
    if (x == other.x) return y > other.y;
    return x > other.x;
  }
  bool operator==(const par& other) const {
    return x == other.x && y == other.y;
  }
  par operator-(const par& b) const { return {x - b.x, y - b.y}; }
  string str() const {
    ostringstream ss;
    ss << "{" << x << "," << y << "}";
    return ss.str();
  }
};
template <class T>
inline vector<T> rv(long long n) {
  vector<T> v(n);
  for (long long i = 0; i < (n); i++) cin >> v[i];
  return v;
}
template <class T>
inline void pv(const vector<T>& v) {
  for (long long i = 0; i < (v.size()); i++) cout << v[i] << " ";
  cout << '\n';
}
template <class T, class U>
inline void amax(T& a, const U& b) {
  if (a < b) a = b;
}
template <class T, class U>
inline void amin(T& a, const U& b) {
  if (a > b) a = b;
}
template <class T, class U>
inline T min(T& a, const U& b) {
  return a > b ? b : a;
}
template <class T, class U>
inline T max(T& a, const U& b) {
  return a < b ? b : a;
}
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244853LL;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
long long GCD(long long a, long long b) {
  if (a < b) swap(a, b);
  while (b) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long rubbernuts[107][107], dribbletits = 0, fatbastard = 0,
                                bustanutz[1000007], junglejumper = 0;
string asschapper;
queue<int> spunkmonkey;
int main() {
  cin >> dribbletits;
  for (int knobgobbler = 1; knobgobbler <= dribbletits; knobgobbler++) {
    cin >> asschapper;
    for (int shitmonger = 1; shitmonger <= dribbletits; shitmonger++)
      rubbernuts[knobgobbler][shitmonger] =
          asschapper[shitmonger - 1] == '1' ? 1 : 1000000007LL;
    rubbernuts[knobgobbler][knobgobbler] = 0;
  }
  cin >> fatbastard;
  for (int blow = 1; blow <= dribbletits; blow++)
    for (int yourwig = 1; yourwig <= dribbletits; yourwig++)
      for (int back = 1; back <= dribbletits; back++)
        if (rubbernuts[yourwig][blow] + rubbernuts[blow][back] <
            rubbernuts[yourwig][back])
          rubbernuts[yourwig][back] =
              rubbernuts[yourwig][blow] + rubbernuts[blow][back];
  for (int whatwhat = 1; whatwhat <= fatbastard; whatwhat++) {
    cin >> bustanutz[whatwhat];
    if (junglejumper == 0) {
      junglejumper = whatwhat;
      spunkmonkey.push(bustanutz[whatwhat]);
    } else if (whatwhat - junglejumper !=
               rubbernuts[bustanutz[junglejumper]][bustanutz[whatwhat]]) {
      spunkmonkey.push(bustanutz[whatwhat - 1]);
      junglejumper = whatwhat - 1;
    }
  }
  spunkmonkey.push(bustanutz[fatbastard]);
  cout << spunkmonkey.size() << "\n";
  while (!spunkmonkey.empty()) {
    cout << spunkmonkey.front() << "\n";
    spunkmonkey.pop();
  }
  return 0;
}
