#include <bits/stdc++.h>
using namespace std;
const int maxm = 1e3 + 10;
char s[maxm][maxm];
int p[maxm];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &p[i]);
    }
    for (int i = 0; i < n; i++) {
      scanf("%s", s[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (s[i][j] == '0') continue;
        for (int k = 0; k < n; k++) {
          if (s[i][k] == '1') {
            s[k][j] = '1';
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (s[i][j] == '1' && p[i] > p[j]) {
          swap(p[i], p[j]);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (i == 0)
        printf("%d", p[i]);
      else
        printf(" %d", p[i]);
    }
    printf("\n");
  }
  return 0;
}
