#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > a;
vector<bool> used;
int n;
vector<int> comp;
void dfs(int v) {
  comp.push_back(v);
  used[v] = true;
  for (int i = 0; i < n; ++i) {
    if (!used[i] && a[v][i]) {
      dfs(i);
    }
  }
}
int main() {
  cin >> n;
  vector<long long> p(n);
  for (int i = 0; i < n; ++i) cin >> p[i];
  a.assign(n, vector<int>(n, 0));
  used.assign(n, false);
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < n; ++j)
      if (s[j] == '1') a[i][j] = 1;
  }
  vector<int> ans(n);
  for (int i = 0; i < n; ++i)
    if (!used[i]) {
      comp.clear();
      dfs(i);
      vector<int> real;
      for (int j = 0; j < comp.size(); ++j) real.push_back(p[comp[j]]);
      sort(real.begin(), real.end());
      sort(comp.begin(), comp.end());
      for (int j = 0; j < real.size(); ++j) {
        int cur = comp[j];
        ans[cur] = real[j];
      }
    }
  for (int i = 0; i < n; ++i) cout << ans[i] << " ";
  return 0;
}
