#include <bits/stdc++.h>
using namespace std;
int n;
int Mark[301];
char a[301][301];
int H[301];
void dfs(int i, int value) {
  Mark[i] = 1;
  H[i] = value;
  for (int j = 1; j <= n; j++)
    if (Mark[j] == 0 && a[i][j] == '1') dfs(j, value);
}
int main() {
  cin >> n;
  int p[n];
  for (int i = 1; i <= n; i++) cin >> p[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) cin >> a[i][j];
    H[i] = i;
  }
  for (int i = 1; i < n; i++)
    if (Mark[i] == 0) dfs(i, i);
  for (int i = 1; i < n; i++)
    for (int j = i + 1; j <= n; j++) {
      if (p[i] > p[j] && H[i] == H[j]) swap(p[i], p[j]);
    }
  for (int i = 1; i <= n; i++) cout << p[i] << " ";
}
