#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const double eps = 1e-9;
const int N = 310;
int a[N];
int p[N];
inline int get(int x) {
  if (p[x] != x) p[x] = get(p[x]);
  return p[x];
}
char s[N][N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; (i) < (n); ++i) scanf("%d", &a[i]);
  for (int i = 0; (i) < (n); ++i) p[i] = i;
  for (int i = 0; (i) < (n); ++i) scanf("%s", s[i]);
  for (int i = 0; (i) < (n); ++i)
    for (int j = 0; (j) < (n); ++j)
      if (s[i][j] == '1') {
        int x = get(i), y = get(j);
        if (x != y) p[x] = y;
      }
  for (int _ = 0; (_) < (n); ++_)
    for (int i = 0; (i) < (n); ++i)
      for (int j = i + 1; j < n; ++j)
        if (get(i) == get(j) && a[i] > a[j]) swap(a[i], a[j]);
  for (int i = 0; (i) < (n); ++i) printf("%d%c", a[i], " \n"[i + 1 == n]);
  return 0;
}
