#include <bits/stdc++.h>
using namespace std;
int d[302][302], n, a[302];
int dd[302], b[302], c[302];
void nhap() {
  string s;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    cin >> s;
    for (int j = 1; j <= n; j++) d[i][j] = s[j - 1] - '0';
  }
}
void dfs(int u) {
  dd[u] = 1;
  c[++c[0]] = u;
  b[c[0]] = a[u];
  for (int i = 1; i <= n; i++)
    if (dd[i] == 0 && d[u][i] == 1) dfs(i);
}
void saplai() {
  sort(b + 1, b + c[0] + 1);
  sort(c + 1, c + c[0] + 1);
  for (int i = 1; i <= c[0]; i++) a[c[i]] = b[i];
}
void xuly() {
  for (int i = 1; i <= n; i++) dd[i] = 0;
  for (int i = 1; i <= n; i++)
    if (dd[i] == 0) {
      c[0] = 0;
      dfs(i);
      saplai();
    }
}
void ghi() {
  for (int i = 1; i <= n; i++) cout << a[i] << " ";
}
int main() {
  nhap();
  xuly();
  ghi();
}
