#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > connected;
int p = 0;
vector<vector<int> > b;
vector<int> visited;
void dfs(int x) {
  connected[p].push_back(x);
  for (int i = 0; i < b[x].size(); i++) {
    if (!visited[b[x][i]]) {
      visited[b[x][i]] = 1;
      dfs(b[x][i]);
    }
  }
}
int main() {
  int n, i;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++) cin >> a[i];
  b.resize(n);
  visited.resize(n);
  for (i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char x;
      cin >> x;
      if (x == '1') b[i].push_back(j);
    }
  }
  p = 0;
  connected.resize(n);
  for (i = 0; i < n; i++) {
    if (visited[i]) continue;
    visited[i] = 1;
    dfs(i);
    p++;
  }
  vector<int> ans(n);
  for (int i = 0; i < p; i++) {
    sort(connected[i].begin(), connected[i].end());
  }
  for (int i = 0; i < p; i++) {
    vector<int> v;
    for (int j = 0; j < connected[i].size(); j++) {
      v.push_back(a[connected[i][j]]);
    }
    sort(v.begin(), v.end());
    for (int j = 0; j < connected[i].size(); j++) {
      ans[connected[i][j]] = v[j];
    }
  }
  for (i = 0; i < n; i++) cout << ans[i] << " ";
}
