#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v[305];
int a[305], p[305], gp[305];
string s;
int fd(int i) {
  if (i != p[i]) p[i] = fd(p[i]);
  return p[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  memset(gp, 0, sizeof(gp));
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i <= n; i++) p[i] = i;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '0') continue;
      p[fd(j)] = p[fd(i)];
    }
  }
  for (int i = 0; i < n; i++) v[fd(i)].push_back(a[i]);
  for (int i = 0; i < n; i++) sort(v[i].begin(), v[i].end());
  for (int i = 0; i < n; i++) cout << v[fd(i)][gp[fd(i)]++] << " ";
  cout << endl;
  return 0;
}
