#include <bits/stdc++.h>
using namespace std;
int v[305], a[305], f[305], p[305], o, i, j, b[305], n;
char s[305];
int getf(int k) { return f[k] == k ? f[k] : f[k] = getf(f[k]); }
int cmp(int i, int j) { return a[i] < a[j]; }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) f[i] = i, scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) {
    scanf("%s", s);
    for (j = 0; j < n; j++)
      if (s[j] == '1') f[getf(i)] = getf(f[j + 1]);
  }
  for (i = 1; i <= n; i++)
    if (!v[i]) {
      p[o = 1] = i;
      for (j = i + 1; j <= n; j++)
        if (getf(i) == getf(j)) p[++o] = j, v[j] = 1;
      sort(p + 1, p + o + 1, cmp);
      b[i] = a[p[1]];
      o = 1;
      for (j = i + 1; j <= n; j++)
        if (getf(i) == getf(j)) b[j] = a[p[++o]];
    }
  for (i = 1; i <= n; i++) cout << b[i] << ' ';
  return 0;
}
