#include <bits/stdc++.h>
using namespace std;
const int MAX = 100010;
const int MOD = 1e9 + 7;
char m[310][310];
int a[310];
int n;
vector<vector<pair<int, int> > > graph;
bool visited[310];
vector<int> v, wt;
void dfs(int u) {
  v.push_back(u);
  wt.push_back(a[u] - 1);
  visited[u] = 1;
  for (int i = 0; i < graph[u].size(); i++) {
    pair<int, int> p = graph[u][i];
    if (!visited[p.second]) {
      dfs(p.second);
    }
  }
}
int main() {
  cin >> n;
  graph = vector<vector<pair<int, int> > >(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) scanf("%s", m[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (m[i][j] == '1') {
        graph[i].push_back(make_pair(a[j] - 1, j));
        graph[j].push_back(make_pair(a[i] - 1, i));
      }
    }
  }
  vector<int> ans(n, 0);
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      v.clear();
      wt.clear();
      dfs(i);
      sort(v.begin(), v.end());
      sort(wt.begin(), wt.end());
      for (int i = 0; i < v.size(); i++) {
        ans[v[i]] = wt[i];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] + 1;
    if (i != n - 1) cout << " ";
  }
  cout << endl;
  return 0;
}
