#include <bits/stdc++.h>
using namespace std;
string s[301];
int a[301];
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    s[i][i] = '0';
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if ((s[i][k] == '1') && (s[k][j] == '1')) s[i][j] = '1';
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int b = i;
    for (int j = i + 1; j < n; j++) {
      if ((s[i][j] == '1') && (a[j] < a[b])) b = j;
    }
    swap(a[b], a[i]);
  }
  for (int i = 0; i < n; i++) cout << a[i] << " ";
  cout << endl;
  return 0;
}
