#include <bits/stdc++.h>
using namespace std;
int mapp[311][311];
int ma[311];
char st[311][311];
int main() {
  int n, i, j, kl, pl, ml;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &ma[i]);
  memset(mapp, 0, sizeof(mapp));
  for (i = 1; i <= n; i++) {
    scanf("%s", st[i]);
    for (j = 0; j < n; j++) {
      if (st[i][j] == '1') mapp[j + 1][i] = mapp[i][j + 1] = 1;
    }
  }
  for (kl = 1; kl <= n; kl++)
    for (i = 1; i <= n; i++) {
      if (mapp[i][kl] == 0) continue;
      for (j = 1; j <= n; j++)
        if (mapp[kl][j]) mapp[i][j] = 1;
    }
  for (i = 1; i <= n; i++)
    for (j = i + 1; j <= n; j++)
      if (mapp[i][j] && ma[i] > ma[j]) {
        kl = ma[i];
        ma[i] = ma[j];
        ma[j] = kl;
      }
  for (i = 1; i < n; i++) printf("%d ", ma[i]);
  printf("%d\n", ma[n]);
  return 0;
}
