#include <bits/stdc++.h>
int a[305], i, j, k, dis[305][305], n;
char s[305];
int main() {
  while (scanf("%d", &n) != EOF) {
    for (i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (i = 1; i <= n; i++) {
      scanf("%s", s);
      for (j = 1; j <= n; j++) dis[i][j] = s[j - 1] - '0';
    }
    for (k = 1; k <= n; k++)
      for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
          if (!dis[i][j]) dis[i][j] = dis[i][k] & dis[k][j];
    for (i = 1; i <= n; ++i)
      for (j = i + 1; j <= n; ++j)
        if (dis[i][j])
          if (a[i] > a[j]) a[i] ^= a[j], a[j] ^= a[i], a[i] ^= a[j];
    for (i = 1; i <= n; ++i) printf("%d%c", a[i], i == n ? '\n' : ' ');
  }
  return 0;
}
