#include <bits/stdc++.h>
using namespace std;
struct node {
  int val, pos;
} a[1000];
int cmp(node a, node b) { return a.val < b.val; }
int ans[450];
char mpp[450][480];
int mp[450][450];
int main() {
  int n;
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i].val);
      a[i].pos = i;
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < n; i++) {
      scanf("%s", mpp[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        mp[i][j] = mpp[i][j] - '0';
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (mp[j][i] == 1 && mp[i][k] == 1) mp[j][k] = 1;
        }
      }
    }
    memset(ans, -1, sizeof(ans));
    for (int i = 0; i < n; i++) {
      int flag = 0;
      for (int j = 0; j < n; j++) {
        if (mp[a[i].pos][j] == 1 && ans[j] == -1) {
          flag = 1;
          ans[j] = a[i].val;
          break;
        }
      }
      if (flag == 0) ans[a[i].pos] = a[i].val;
    }
    for (int i = 0; i < n; i++) {
      printf("%d ", ans[i]);
    }
    printf("\n");
  }
}
