#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
int n;
int p[305], fa[305];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
  n = read();
  for (int i = 1; i <= n; i++) p[i] = read();
  for (int i = 1; i <= n; i++) fa[i] = i;
  char ch[305];
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + 1);
    for (int j = 1; j <= n; j++)
      if (ch[j] == '1') {
        int p = find(i), q = find(j);
        if (p != q) fa[p] = q;
      }
  }
  for (int i = 1; i <= n; i++) {
    int t = i;
    for (int j = i + 1; j <= n; j++)
      if (find(j) == find(i) && p[j] < p[t]) t = j;
    swap(p[i], p[t]);
  }
  for (int i = 1; i <= n; i++) printf("%d ", p[i]);
  return 0;
}
