#include <bits/stdc++.h>
using namespace std;
long long n;
long long p[400];
string a[400];
vector<long long> val;
vector<int> ind;
int visited[400];
void dfs(long long i) {
  visited[i] = 1;
  val.push_back(p[i]);
  ind.push_back(i);
  long long j;
  for (j = 0; j < n; j++) {
    if (a[i][j] == '1') {
      if (visited[j] == 0) {
        dfs(j);
      }
    }
  }
}
int main() {
  long long i, j, k, l;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    val.clear();
    ind.clear();
    if (visited[i] == 0) dfs(i);
    sort(val.begin(), val.end());
    sort(ind.begin(), ind.end());
    for (int k = 0; k < ind.size(); k++) {
      p[ind[k]] = val[k];
    }
  }
  for (i = 0; i < n; i++) {
    cout << p[i] << " ";
  }
  return 0;
}
