#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int N = 305;
int haifei;
int a[N], d[N][N];
int ddd;
int hehe;
char p[N][N];
int main() {
  int n, t;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%s", p[i]);
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (p[i][k] == '1' && p[k][j] == '1') p[i][j] = '1';
      }
    }
  }
  bool ok = 1;
  while (ok) {
    ok = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (p[i][j] == '1' && a[i] > a[j]) {
          swap(a[i], a[j]);
          ok = 1;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (i != 0) putchar(' ');
    printf("%d", a[i]);
  }
  puts("");
  return 0;
}
