#include <bits/stdc++.h>
using namespace std;
int arr[305], s[305][305];
int main() {
  int n, m, i, j, k, x, y;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      scanf("%1d", &s[i][j]);
    }
  }
  for (k = 0; k < n; k++)
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (s[i][k] && s[k][j]) s[i][j] = 1;
      }
    }
  for (i = 0; i < n; i++) {
    int mi = -1, id;
    for (j = i + 1; j < n; j++) {
      if (arr[j] > arr[i]) continue;
      if (mi == -1 || arr[j] < mi) {
        if (s[i][j]) {
          mi = arr[j], id = j;
        }
      }
    }
    if (mi != -1) {
      swap(arr[i], arr[id]);
    }
  }
  printf("%d", arr[0]);
  for (i = 1; i < n; i++) printf(" %d", arr[i]);
  puts("");
}
