#include <bits/stdc++.h>
using namespace std;
const double Pi = acos(-1);
const long long oo = (long long)(1e17);
vector<int> p;
vector<bool> used;
vector<vector<int>> g;
vector<vector<int>> comp;
vector<vector<int>> idx;
int cnt, n;
void dfs(int u, int com) {
  comp[com].push_back(p[u]);
  idx[com].push_back(u);
  used[u] = 1;
  for (int v : g[u])
    if (!used[v]) dfs(v, com);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  p.resize(n);
  g.resize(n);
  for (int i = 0; i < n; i++) cin >> p[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      char x;
      cin >> x;
      if (x == '1') g[i].push_back(j);
    }
  used.resize(n);
  comp.resize(n);
  idx.resize(n);
  for (int i = 0; i < n; i++)
    if (!used[i]) {
      dfs(i, cnt);
      cnt++;
    }
  for (int i = 0; i < cnt; i++) {
    sort(comp[i].begin(), comp[i].end());
    sort(idx[i].begin(), idx[i].end());
  }
  vector<int> ans(n);
  for (int i = 0; i < cnt; i++)
    for (int j = 0; j < comp[i].size(); j++) ans[idx[i][j]] = comp[i][j];
  for (int x : ans) cout << x << ' ';
  return 0;
}
