#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, a[N], A[N][N];
string s;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    for (int j = 0; j < n; ++j) {
      if (s[j] == '1')
        A[i][j + 1] = 1;
      else
        A[i][j + 1] = 0;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (A[j][i] == 0) continue;
      for (int k = 1; k <= n; ++k) {
        if (A[i][k]) A[j][k] = 1;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (A[i][j] && a[i] > a[j]) swap(a[i], a[j]);
    }
  }
  for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
}
