#include <bits/stdc++.h>
using namespace std;
const int maxn = 300;
int a[maxn + 5], ans[maxn + 5], q[maxn + 5], p[maxn + 5], father[maxn + 5];
char b[maxn + 5][maxn + 5];
int n;
int _find(int x) {
  if (x == father[x]) return x;
  father[x] = _find(father[x]);
  return father[x];
}
void _merge(int u, int v) {
  u = _find(u);
  v = _find(v);
  father[u] = v;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) father[i] = i;
  memset(ans, 0, sizeof(ans));
  for (int i = 0; i < n; i++) scanf("%s", b[i]);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (b[i - 1][j - 1] == '1') _merge(i, j);
  for (int i = 1; i <= n; i++)
    if (!ans[i]) {
      int x = _find(i);
      p[0] = i;
      q[0] = a[i];
      int num = 1;
      for (int j = i + 1; j <= n; j++)
        if (_find(j) == x) {
          p[num] = j;
          q[num] = a[j];
          num++;
        }
      sort(q, q + num);
      for (int j = 0; j < num; j++) ans[p[j]] = q[j];
    }
  for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
  return 0;
}
