#include <bits/stdc++.h>
using namespace std;
vector<int> adj[510];
vector<bool> vis(510);
vector<int> bfs(int s) {
  vector<int> res;
  queue<int> q;
  q.push(s);
  vis[s] = true;
  while (!q.empty()) {
    s = q.front();
    q.pop();
    res.push_back(s);
    for (auto i : adj[s]) {
      if (!vis[i]) {
        vis[i] = true;
        q.push(i);
      }
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> p(n + 1), ans(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  string s;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    for (int j = 1; j <= n; j++) {
      if (s[j - 1] == '1') {
        adj[i].push_back(j);
      }
    }
  }
  vector<vector<int>> comp;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      comp.push_back(bfs(i));
    }
  }
  for (auto v : comp) {
    vector<int> val, ind;
    for (auto i : v) {
      val.push_back(p[i]);
      ind.push_back(i);
    }
    sort(val.begin(), val.end());
    sort(ind.begin(), ind.end());
    for (int i = 0; i < val.size(); i++) {
      ans[ind[i]] = val[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << ' ';
  }
  return 0;
}
