#include <bits/stdc++.h>
using namespace std;
long long get() {
  char c = getchar();
  long long x = 0LL;
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    x *= 10LL;
    x += (c - '0');
    c = getchar();
  }
  return x;
}
int group[300];
int a[300];
int cmp;
vector<int> g[300];
int n;
void dfs(int i) {
  group[i] = cmp;
  for (int j = 0; j < g[i].size(); j++) {
    if (group[g[i][j]] == -1) {
      dfs(g[i][j]);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      if (c - '0' == 1) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  for (int i = 0; i < n; i++) group[i] = -1;
  for (int i = 0; i < n; i++) {
    if (group[i] == -1) {
      dfs(i);
      cmp++;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++)
      if (a[j] < a[i] && group[j] == group[i]) {
        swap(a[i], a[j]);
      }
  }
  for (int i = 0; i < n; i++) cout << a[i] << " ";
  return 0;
}
