#include <bits/stdc++.h>
using namespace std;
int ori[305], fa[305];
string in;
void init() {
  for (int i = 1; i <= 300; i++) fa[i] = i;
}
int Find(int x) { return fa[x] = fa[x] == x ? x : Find(fa[x]); }
int main() {
  init();
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &ori[i]);
  for (int i = 1; i <= n; i++) {
    cin >> in;
    for (int j = i; j < n; j++) {
      if (in[j] == '1') {
        int fi = Find(i), fj = Find(j + 1);
        if (fi != fj) fa[fj] = fa[fi];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    int pos = i, now = ori[i];
    for (int j = i + 1; j <= n; j++) {
      if (Find(i) == Find(j) && ori[j] < now) {
        pos = j, now = ori[j];
      }
    }
    ori[pos] = ori[i];
    ori[i] = now;
  }
  for (int i = 1; i <= n; i++) printf("%d ", ori[i]);
  printf("\n");
  return 0;
}
