#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
const long long N = 405;
vector<long long> adj[N], a(N), vis(N), vv, vv1;
void dfs(long long u) {
  vis[u] = 1;
  vv.push_back(a[u]);
  vv1.push_back(u);
  for (long long v : adj[u]) {
    if (!vis[v]) dfs(v);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  char gg[n + 1][n + 1];
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> gg[i][j];
      if (gg[i][j] == '1') {
        adj[i].push_back(j);
        adj[j].push_back(i);
      }
    }
  }
  vector<long long> ans(n + 1);
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      sort((vv).begin(), (vv).end());
      sort((vv1).begin(), (vv1).end());
      for (long long j = 0;
           j < min((long long)vv.size(), (long long)vv1.size()); j++)
        ans[vv1[j]] = vv[j];
      vv.clear();
      vv1.clear();
    }
  }
  for (long long i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
