#include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(int m);
const int mod = 998244353;
const int N = 1e6 + 16, M = N;
const int LN = 1e9;
long long dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
struct cmp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
    int len1 = a.second - a.first + 1;
    int len2 = b.second - b.first + 1;
    if (len1 == len2) {
      return a.first < b.first;
    }
    return len1 > len2;
  }
};
long long fac[N], inv[N], b[N];
int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((long long)result * base) % mod;
    base = ((long long)base * base) % mod;
    exp >>= 1;
  }
  return result;
}
void pre() {
  int i;
  fac[0] = inv[0] = 1;
  fac[1] = inv[1] = 1;
  for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) {
    fac[i] = (i * fac[i - 1]) % mod;
    inv[i] = (mpow(i, mod - 2) * inv[i - 1]) % mod;
  }
}
long long C(int n, int r) {
  if (r > n) return 0;
  long long ans = fac[n];
  ans *= inv[r];
  ans %= mod;
  ans *= inv[n - r];
  ans %= mod;
  return ans;
}
long long n;
vector<int> permutation;
int chosen[100] = {0};
long long power(long long a, long long b) {
  long long ans = 1;
  while (b--) ans *= a;
  return ans;
}
long long k;
long long m;
vector<int> positions[1005];
int bk[1005];
int link[N], size[N];
bool comparator(tuple<int, int, int> a, tuple<int, int, int> b) {
  if (get<0>(a) != get<0>(b)) {
    return get<0>(a) > get<0>(b);
  }
  return (get<1>(a)) < (get<1>(b));
}
vector<long long> primes;
void sieve() {
  int n = 1e6;
  int primess[n];
  memset(primess, 0, sizeof(primess));
  for (int i = 2; i * i <= n; i++) {
    if (primess[i] == 0) {
      primes.push_back(i);
      for (int j = 0; j < n; j++) {
        if (i * (j + 1) >= n) break;
        primess[i * (j + 1)] = -1;
      }
    }
  }
}
int visited[1000006] = {0};
vector<int> adj[1000006];
int cnt = 0;
vector<int> ans;
int mandist(pair<int, int> x1, pair<int, int> x2) {
  return abs(x1.first - x2.first) + abs(x1.second - x2.second);
}
void dfs(int s) {
  if (visited[s] == 1) return;
  visited[s] = 1;
  for (auto xx : adj[s]) {
    if (visited[xx] == 0) dfs(xx);
  }
  ans.push_back(s);
}
void solve() {
  cin >> n;
  vector<int> a1(n);
  for (int i = 0; i < n; i++) cin >> a1[i];
  for (int i = 0; i < n; i++) {
    string s1;
    cin >> s1;
    for (int j = 0; j < n; j++) {
      char x = s1[j];
      if (x == '1') {
        adj[i].push_back(j);
      }
    }
  }
  vector<int> values;
  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      ans.clear();
      values.clear();
      dfs(i);
      for (auto xx : ans) {
        values.push_back(a1[xx]);
      }
      sort(values.begin(), values.end());
      sort(ans.begin(), ans.end());
      for (int j = 0; j < values.size(); j++) {
        a1[ans[j]] = values[j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a1[i] << ' ';
  }
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
}
