#include <bits/stdc++.h>
using namespace std;
int a[400], f[400][400], u[400], n;
vector<int> x, y;
inline void ope(int k) {
  u[k] = 1;
  x.push_back(k);
  y.push_back(a[k]);
  for (int i = 1; i <= n; i++)
    if (f[i][k] && !u[i]) ope(i);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= n; j++) f[i][j] = s[j - 1] - 48;
  }
  for (int i = 1; i <= n; i++) {
    if (!u[i]) {
      ope(i);
      sort(x.begin(), x.end());
      sort(y.begin(), y.end());
      for (int j = 0; j < x.size(); j++) a[x[j]] = y[j];
      x.clear();
      y.clear();
    }
  }
  for (int i = 1; i < n; i++) cout << a[i] << ' ';
  cout << a[n] << endl;
  return 0;
}
