#include <bits/stdc++.h>
using namespace std;
const int MAX = 350;
int n;
int arr[MAX];
int ans[MAX];
char adj[MAX][MAX];
bool vis[MAX];
struct comp {
  vector<int> vals, idx;
  comp() {
    vals.clear();
    idx.clear();
  }
};
void dfs(int node, comp &add) {
  if (vis[node]) return;
  vis[node] = 1;
  add.vals.push_back(arr[node]);
  add.idx.push_back(node);
  for (int i = 0; i < n; i++) {
    if (adj[node][i] == '1') {
      dfs(i, add);
    }
  }
}
vector<comp> a;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> arr[i];
  }
  for (int i = 0; i < (int)(n); ++i) {
    cin >> adj[i];
  }
  for (int i = 0; i < (int)(n); ++i) {
    if (!vis[i]) {
      comp tmp;
      dfs(i, tmp);
      a.push_back(tmp);
    }
  }
  for (int i = 0; i < ((int)((a).size())); ++i) {
    sort(((a[i].vals).begin()), ((a[i].vals).end()));
    sort(((a[i].idx).begin()), ((a[i].idx).end()));
  }
  for (int i = 0; i < ((int)((a).size())); ++i) {
    for (int j = 0; j < ((int)((a[i].idx).size())); ++j) {
      ans[a[i].idx[j]] = a[i].vals[j];
    }
  }
  for (int i = 0; i < (int)(n); ++i) {
    cout << ans[i] << ' ';
  }
}
