#include <bits/stdc++.h>
using namespace std;
vector<int> v[100001];
int a[100001];
int visited[100001];
vector<int> w;
vector<int> z;
void dfs(int b) {
  visited[b] = 1;
  int i;
  w.push_back(a[b]);
  z.push_back(b);
  for (i = 0; i < v[b].size(); i++) {
    int x = v[b][i];
    if (visited[x] == 0) dfs(x);
  }
}
int main() {
  int n, i, j, t;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  char s[100001];
  for (i = 1; i <= n; i++) {
    scanf("%s", s);
    for (j = 0; j < n; j++) {
      if (s[j] == '1') v[i].push_back(j + 1);
    }
  }
  for (i = 1; i <= n; i++) {
    if (visited[i] == 0) dfs(i);
    sort(w.begin(), w.end());
    sort(z.begin(), z.end());
    for (j = 0; j < w.size(); j++) a[z[j]] = w[j];
    w.clear();
    z.clear();
  }
  for (i = 1; i <= n; i++) printf("%d ", a[i]);
  printf("\n");
}
