#include <bits/stdc++.h>
using namespace std;
int a[305][305];
int p[305];
int main() {
  int n;
  cin >> 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++) {
      char c;
      cin >> c;
      a[i][j] = c - '0';
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (a[i][k] && a[k][j]) a[i][j] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    int index = i;
    for (int j = i + 1; j <= n; j++) {
      if (a[i][j] && p[j] < p[index]) index = j;
    }
    if (index != i) swap(p[i], p[index]);
  }
  for (int i = 1; i <= n; i++) cout << p[i] << " ";
  return 0;
}
