#include <bits/stdc++.h>   using namespace std;   bool PIIfs(const pair<int, int>& a, const pair<int, int>& b) {     return a.first < b.first || (a.first == b.first && a.second < b.second);   }   bool PIIsf(const pair<int, int>& a, const pair<int, int>& b) {     return a.second < b.second || (a.second == b.second && a.first < b.first);   }   bool PIIFS(const pair<int, int>& a, const pair<int, int>& b) {     return a.first > b.first || (a.first == b.first && a.second > b.second);   }   bool PIISF(const pair<int, int>& a, const pair<int, int>& b) {     return a.second > b.second || (a.second == b.second && a.first > b.first);   }   bool PIIfS(const pair<int, int>& a, const pair<int, int>& b) {     return a.first < b.first || (a.first == b.first && a.second > b.second);   }   bool PIIsF(const pair<int, int>& a, const pair<int, int>& b) {     return a.second < b.second || (a.second == b.second && a.first > b.first);   }   bool PIIFs(const pair<int, int>& a, const pair<int, int>& b) {     return a.first > b.first || (a.first == b.first && a.second < b.second);   }   bool PIISf(const pair<int, int>& a, const pair<int, int>& b) {     return a.second > b.second || (a.second == b.second && a.first < b.first);   }   template <typename T>   inline T max(T a, T b, T c) {     return max(max(a, b), c);   }   template <typename T>   inline T max(T a, T b, T c, T d) {     return max(max(a, b, c), d);   }   template <typename T>   inline T max(T a, T b, T c, T d, T e) {     return max(max(a, b, c, d), e);   }   template <typename T>   inline T min(T a, T b, T c) {     return min(min(a, b), c);   }   template <typename T>   inline T min(T a, T b, T c, T d) {     return min(min(a, b, c), d);   }   template <typename T>   inline T min(T a, T b, T c, T d, T e) {     return min(min(a, b, c, d), e);   }   template <typename T>   inline istream& RD(T& x) {     return cin >> x;   }   template <typename T, typename U>   inline istream& RD(T& a, U& b) {     return cin >> a >> b;   }   template <typename T, typename U, typename V>   inline istream& RD(T& a, U& b, V& c) {     return cin >> a >> b >> c;   }   template <typename T, typename U, typename V, typename W>   inline istream& RD(T& a, U& b, V& c, W& d) {     return cin >> a >> b >> c >> d;   }   template <typename T, typename U, typename V, typename W, typename X>   inline istream& RD(T& a, U& b, V& c, W& d, X& e) {     return cin >> a >> b >> c >> d >> e;   }   template <typename T>   void checkmin(T& a, const T& b) {     if (b < a) a = b;   }   template <typename T>   void checkmax(T& a, const T& b) {     if (b > a) a = b;   }   int n;   void dfs(const vector<string>& vs, bitset<300>& mark, int node,            vector<int>& ans) {     if (mark.test(node)) return;     mark.set(node);     ans.push_back(node);     for (int i = 0; i < n; i++)       if (vs[node][i] ==  1 ) dfs(vs, mark, i, ans);   }   void perm(vector<int>& vi, vector<int>& si) {     int x = si.size();     vector<int> tmp(x);     for (int i = 0; i < (int)(x); ++i) tmp[i] = vi[si[i]];     sort(tmp.begin(), tmp.end());     sort(si.begin(), si.end());     for (int i = 0; i < (int)(x); ++i) vi[si[i]] = tmp[i];   }   int main() {     std::ios::sync_with_stdio(false);     cin >> n;     vector<int> vi(n);     for (int i = 0; i < (int)(n); ++i) cin >> vi[i];     vector<string> vs;     string s;     for (int i = 0; i < (int)(n); ++i) {       cin >> s;       vs.push_back(s);     }     vector<set<int> > circles;     bitset<300> found;     for (int i = 0; i < n; i++) {       vector<int> ans;       dfs(vs, found, i, ans);       if (ans.size() > 1) {         perm(vi, ans);       }     }     for (__typeof__(vi.begin()) it = vi.begin(); it != vi.end(); ++it)       cout << *it <<    ;     cout <<   n ;     return 0;   }