#include <bits/stdc++.h>
using namespace std;
void output(vector<vector<int> > &g) {
  for (int i = 0; i < g.size(); i++) {
    (0);
    for (int v : g[i]) (0);
    (0);
  }
}
void _(string s) { cout << s << endl; }
vector<vector<int> > g;
vector<int> p, used, comp;
void dfs(int v) {
  used[v] = true;
  comp.push_back(v);
  for (int to : g[v])
    if (!used[to]) dfs(to);
}
void process_component() {
  vector<int> e;
  for (int i : comp) e.push_back(p[i]);
  sort(e.begin(), e.end());
  sort(comp.begin(), comp.end());
  for (int i = 0; i < e.size(); i++) p[comp[i]] = e[i];
}
int main() {
  int n;
  cin >> n;
  p.resize(n);
  for (int i = 0; i < n; i++) cin >> p[i];
  g.resize(n);
  used.resize(n);
  fill(used.begin(), used.end(), 0);
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++)
      if (s[j] == '1') g[i].push_back(j);
  }
  for (int i = 0; i < n; i++)
    if (!used[i]) {
      comp.clear();
      dfs(i);
      process_component();
    }
  for (int e : p) cout << e << ' ';
  cout << endl;
  return 0;
}
