#include <bits/stdc++.h>
using namespace std;
int n, a[305];
char s[305][305];
bool f[305][305];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= n; j++) {
      if (s[i][j] - 48 == 1) {
        f[i][j] = f[j][i] = true;
      }
    }
  }
  for (int i = 1; i <= n; i++) f[i][i] = true;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (f[i][k] == true && f[k][j] == true) f[i][j] = f[j][i] = true;
      }
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      if (f[i][j] == true && a[i] > a[j]) {
        swap(a[i], a[j]);
      }
    }
    printf("%d ", a[i]);
  }
}
