#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:33554432")
using namespace std;
int n;
int p[1 << 9];
int a[1 << 9][1 << 9];
char buf[1 << 9];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &p[i]);
  for (int i = 0; i < n; ++i) {
    scanf("%s", buf);
    for (int j = 0; j < n; ++j) a[i][j] = buf[j] - '0';
  }
  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) a[i][j] |= a[i][k] & a[k][j];
  for (int i = 0; i < n; ++i) a[i][i] = 1;
  for (int i = 0; i < n; ++i) {
    int MIN = i;
    for (int j = i; j < n; ++j)
      if (p[j] < p[MIN] && a[i][j]) MIN = j;
    swap(p[MIN], p[i]);
  }
  for (int i = 0; i < n; ++i) printf("%d ", p[i]);
  printf("\n");
  return 0;
}
