#include <bits/stdc++.h>
using namespace std;
queue<int> que;
const int maxn = 305;
int father[maxn], num[maxn];
char mat[maxn][maxn];
int minn(int a, int b) { return a < b ? a : b; }
int maxed(int a, int b) { return a > b ? a : b; }
int findn(int n) {
  if (n != father[n]) father[n] = findn(father[n]);
  return father[n];
}
void swa(int &a, int &b) {
  int tem = a;
  a = b;
  b = tem;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> num[i];
  }
  for (int i = 1; i <= n; i++) father[i] = i;
  for (int i = 0; i < n; i++) scanf("%s", mat[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      if (mat[i][j] == '1' && i >= j) {
        int pa = findn(i + 1);
        int pb = findn(j + 1);
        father[pa] = pb;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    int minx = num[i];
    int ind;
    for (int j = i + 1; j <= n; j++) {
      if (num[j] < minx) {
        int pa = findn(i);
        int pb = findn(j);
        if (pa == pb) {
          minx = num[j];
          ind = j;
        }
      }
    }
    if (minx < num[i]) swa(num[i], num[ind]);
  }
  cout << num[1];
  for (int i = 2; i <= n; i++) cout << " " << num[i];
  cout << endl;
}
