#include <bits/stdc++.h>
using namespace std;
int a[400];
string can[400];
vector<int> comps[400];
vector<int> comps2[400];
int vis[500];
void dfs(int cnode, int ccmp) {
  if (vis[cnode]) return;
  vis[cnode] = 1;
  comps[ccmp].push_back(cnode);
  comps2[ccmp].push_back(a[cnode]);
  for (int i = 0; i < (int)can[cnode].size(); i++) {
    if (can[cnode][i] == '1') dfs(i, ccmp);
  }
}
int f[500];
int main() {
  int n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> can[i];
  }
  int cmp = 0;
  for (i = 0; i < n; i++) {
    if (vis[i]) continue;
    dfs(i, cmp);
    sort(comps2[cmp].begin(), comps2[cmp].end());
    sort(comps[cmp].begin(), comps[cmp].end());
    for (int j = 0; j < (int)comps2[cmp].size(); j++) {
      f[comps[cmp][j]] = comps2[cmp][j];
    }
    cmp++;
  }
  for (i = 0; i < n; i++) cout << f[i] << " ";
  cout << endl;
  return 0;
}
