#include <bits/stdc++.h>   using namespace std;   const int Maxn = 1e6 + 1;   const int Mod = 1e9 + 7;   const long long int MOD = 1000000007LL;   const long long int MAX = 100010LL;   template <typename T>   T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <typename T>   T power(T x, T y, long long int m = MOD) {     T ans = 1;     x %= m;     while (y > 0) {       if (y & 1ll) ans = (ans * x) % m;       y >>= 1ll;       x = (x * x) % m;     }     return ans % m;   }   template <typename T, typename T1>   T mod(T x, T1 p) {     x %= p;     if (x < 0) x += p;     return x;   }   void leastPrimeFactor(vector<int> &least_prime, int n) {     least_prime[1] = 1;     for (int i = 2; i <= n; i++) {       if (least_prime[i] == 0) {         least_prime[i] = i;         for (int j = 2 * i; j <= n; j += i)           if (least_prime[j] == 0) least_prime[j] = i;       }     }   }   bool dfs(vector<vector<int>> &graph, vector<int> &g, int i) {     int prev = 0;     queue<int> q;     q.push(i);     while (!q.empty()) {       int n = q.size();       while (n--) {         int t = q.front();         q.pop();         if (g[t] == -1) {           g[t] = prev;           for (int j : graph[t]) q.push(j);         }         if (g[t] != prev) return 0;       }       prev ^= 1;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<vector<int>> adj(n);     vector<vector<int>> mat(n, vector<int>(n, 0));     int cnt = n;     for (int j = 0; j < n; j++) {       string s;       cin >> s;       for (int i = 0; i < s.size(); i++)         if (s[i] ==  1 ) adj[j].push_back(i);     }     int m;     cin >> m;     vector<int> p(m);     for (int i = 0; i < m; i++) cin >> p[i], p[i]--;     for (int i = 0; i < n; i++) {       queue<int> q;       q.push(i);       int dis = 0;       vector<int> vis(n, 0);       vis[i] = 1;       while (!q.empty()) {         int nu = q.size();         while (nu--) {           int no = q.front();           q.pop();           mat[i][no] = dis;           for (int &ne : adj[no])             if (!vis[ne]) vis[ne] = 1, q.push(ne);         }         dis++;       }     }     vector<int> ans;     ans.push_back(p[0]);     int i = 0;     while (i < p.size()) {       int next = i + 1;       for (int j = 0; j < n; j++)         if (i + mat[p[i]][j] < p.size() && p[i + mat[p[i]][j]] == j)           next = max(next, i + mat[p[i]][j]);       if (next < p.size()) ans.push_back(p[next]);       i = next;     }     cout << ans.size() << endl;     for (int &i : ans) cout << i + 1 <<    ;     cout << endl;   }   