#include <bits/stdc++.h>
int n;
int a[300 + 10], map[300 + 10][300 + 10], pl[300 + 10];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    pl[a[i]] = i;
  }
  char s[300 + 10];
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < n; j++) map[i][j] = s[j] - '0';
  }
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (map[i][k] && map[k][j]) map[i][j] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      if (map[pl[i]][j] && i < a[j]) {
        if (pl[i] > j) {
          a[pl[i]] = a[j];
          pl[a[j]] = pl[i];
          a[j] = i;
          break;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) printf("%d ", a[i]);
  return 0;
}
