#include <bits/stdc++.h>
using namespace std;
int n;
int a[305];
vector<int> pos[305];
int ans[305], fa[305];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void Union(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y) fa[y] = x;
}
void solve(int x) {
  vector<int> val;
  for (int i = 0; i < pos[x].size(); i++) val.push_back(a[pos[x][i]]);
  sort(pos[x].begin(), pos[x].end());
  sort(val.begin(), val.end());
  for (int i = 0; i < pos[x].size(); i++) ans[pos[x][i]] = val[i];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    fa[i] = i;
  }
  char s[305];
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= n; j++) {
      if (s[j] == '1') Union(i, j);
    }
  }
  for (int i = 1; i <= n; i++) {
    pos[find(i)].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (pos[i].size() != 0) solve(i);
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
