#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, std::pair<T, U> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U>& a) {
  in >> a.first >> a.second;
  return in;
}
inline string inttos(long long a) {
  std::stringstream soso;
  soso << a;
  return soso.str();
}
inline long long stoint(const std::string& s) {
  std::stringstream soso;
  soso << s;
  long long x;
  soso >> x;
  return x;
}
inline std::string inttobin(long long x) {
  return std::bitset<8>(x).to_string();
}
void swp(long long& a, long long& b) {
  auto temp = a;
  a = b;
  b = temp;
}
const long long V = 102;
long long graph[V][V];
long long dist[V][V];
void floydWarshall(long long n) {
  long long i, j, k;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) dist[i][j] = graph[i][j];
  for (k = 0; k < n; k++) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (dist[i][k] + dist[k][j] < dist[i][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
}
void solve() {
  long long n;
  cin >> n;
  string s;
  for (long long i = 0; i < n; i++) {
    cin >> s;
    for (long long j = 0; j < n; j++) {
      graph[i][j] = s[j] - '0';
      if (graph[i][j] == 0 && i != j) {
        graph[i][j] = 1e18;
      }
    }
  }
  floydWarshall(n);
  long long m;
  cin >> m;
  vector<long long> v(m);
  for (long long i = 0; i < m; i++) {
    cin >> v[i];
  }
  long long start = v[0], index = 0;
  vector<long long> ans;
  ans.push_back(start);
  for (long long i = 1; i < m; i++) {
    if (dist[start - 1][v[i] - 1] != i - index) {
      index = i - 1;
      start = v[index];
      ans.push_back(start);
    }
  }
  ans.push_back(v[m - 1]);
  cout << (long long)ans.size() << "\n";
  for (auto& A : ans) cout << A << " ";
  cout << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q = 1;
  for (long long i = 0; i < q; i++) {
    solve();
  }
  return 0;
}
