#include <bits/stdc++.h>
using namespace std;
int N;
int p[310];
bool graph[310][310];
char buf[310];
int main() {
  int i, j, k;
  cin >> N;
  for ((i) = 0; (i) < (int)(N); (i)++) scanf("%d", &p[i]);
  for ((i) = 0; (i) < (int)(N); (i)++) {
    scanf("%s", buf);
    for ((j) = 0; (j) < (int)(N); (j)++)
      graph[i][j] = (i == j || buf[j] == '1');
  }
  for ((k) = 0; (k) < (int)(N); (k)++)
    for ((i) = 0; (i) < (int)(N); (i)++)
      for ((j) = 0; (j) < (int)(N); (j)++)
        if (graph[i][k] && graph[k][j]) graph[i][j] = true;
  for ((i) = 0; (i) < (int)(N); (i)++)
    for (j = i + 1; j < N; j++)
      if (graph[i][j] && p[j] < p[i]) swap(p[i], p[j]);
  for ((i) = 0; (i) < (int)(N); (i)++) {
    printf("%d", p[i]);
    if (i == N - 1)
      printf("\n");
    else
      printf(" ");
  }
  return 0;
}
