#include <bits/stdc++.h>
using namespace std;
int n;
int p[305], pos[305];
string A[305];
bool put[305];
int main() {
  scanf("%d", &(n));
  for (int i = 0; i < n; i++) {
    scanf("%d", &(p[i]));
    pos[p[i]] = i;
  }
  for (int i = 0; i < n; i++) cin >> A[i];
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        A[i][j] = (A[i][k] == '1' && A[k][j] == '1' ? '1' : A[i][j]);
  int posi;
  for (int i = 1; i <= n; i++) {
    posi = pos[i];
    for (int j = 0; j < n; j++)
      if (A[posi][j] == '1' && j < posi && p[j] > i && !put[j]) {
        swap(pos[i], pos[p[j]]);
        swap(p[posi], p[j]);
        break;
      }
    put[pos[i]] = 1;
  }
  for (int i = 0; i < n; i++) printf("%d%c", p[i], (i == n - 1 ? '\n' : ' '));
  return 0;
}
