#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int fa[500];
bool vis[500];
struct node {
  int val, no;
} a[500];
int fid(int x) {
  if (x == fa[x]) return x;
  return fa[x] = fid(fa[x]);
}
void attach(int u, int v) {
  if (fid(u) == fid(v)) return;
  fa[fid(u)] = v;
}
bool cmp(const node &aa, const node &bb) { return aa.val < bb.val; }
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i].val);
    a[i].no = fa[i] = i;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '1') attach(i, j);
    }
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (vis[j] == 0 && fid(a[j].no) == fid(i)) {
        vis[j] = 1, cout << a[j].val << ' ';
        break;
      }
  return 0;
}
