#include <bits/stdc++.h>
using namespace std;
struct node {
  int num;
  int pos;
} a[311];
int f[311];
int find(int x) {
  if (x != f[x]) f[x] = find(f[x]);
  return f[x];
}
void join(int x, int y) {
  int fx, fy;
  fx = find(x);
  fy = find(y);
  if (fx != fy) f[fx] = fy;
}
bool cmp(node a, node b) { return a.num < b.num; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i].num);
    a[i].pos = i;
  }
  for (int i = 1; i <= n; i++) f[i] = i;
  getchar();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char flag;
      scanf("%c", &flag);
      if (j > i && flag == '1') join(i, j);
    }
    getchar();
  }
  int used[311];
  memset(used, -1, sizeof(used));
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (used[j] == -1) {
        if (find(a[i].pos) == find(j)) {
          used[j] = a[i].num;
          break;
        }
      }
    }
  }
  for (int i = 1; i < n; i++) printf("%d ", used[i]);
  printf("%d\n", used[n]);
  return 0;
}
