#include <bits/stdc++.h>
using namespace std;
int n;
int TEST_NO;
int a[303];
int g[303][303];
int vis[303];
void precompute() {}
void read() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < n; ++j) {
      g[i][j] = s[j] == '1';
    }
  }
}
void preprocess() {}
void go(int v, int m) {
  vis[v] = m;
  for (int i = 0; i < n; ++i) {
    if (!vis[i] and g[v][i]) go(i, m);
  }
}
void solve() {
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) go(i, i + 1);
  }
  for (int i = 0; i < n; ++i) {
    int mt = vis[i];
    for (int j = i; j < n; ++j) {
      if (mt == vis[j] and a[j] < a[i]) swap(a[i], a[j]);
    }
    printf("%d ", a[i]);
  }
}
int main() {
  precompute();
  int t;
  t = 1;
  for (TEST_NO = 1; TEST_NO <= t; TEST_NO++) {
    read();
    preprocess();
    solve();
  }
  return 0;
}
