#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
vector<int> A, B[MAX], C, D;
int visited[MAX];
int n, m;
void DFS(int u) {
  visited[u] = 1;
  C.push_back(u);
  for (int i = 0; i < B[u].size(); i++) {
    int current = B[u][i];
    if (visited[current] == 0) DFS(current);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  A.clear();
  for (int i = 0; i <= n; i++) visited[i] = 0;
  string s;
  for (int i = 0; i < n; i += 1) {
    int val;
    cin >> val;
    A.push_back(val);
  }
  for (int i = 0; i < MAX; i++) B[i].clear();
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        B[i + 1].push_back(j + 1);
        B[j + 1].push_back(i + 1);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    C.clear();
    D.clear();
    if (visited[i] == 0) {
      DFS(i);
      for (int j = 0; j < C.size(); j++) {
        D.push_back(A[C[j] - 1]);
      }
      sort(C.begin(), C.end());
      sort(D.begin(), D.end());
      for (int j = 0; j < C.size(); j++) {
        A[C[j] - 1] = D[j];
      }
    }
  }
  for (int i = 0; i < A.size(); i++) printf("%d ", A[i]);
  printf("\n");
  return 0;
}
