#include <bits/stdc++.h>
using namespace std;
bool v[301][301], v1[301][301] = {0}, v2[301] = {0};
int p, n;
void Go(int u) {
  v1[p][u] = 1;
  for (int i = 1; i <= n; i++) {
    if (v[u][i] == 1 && v1[p][i] == 0) {
      v1[p][i] = 1;
      Go(i);
    }
  }
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  int i, j, x[301], t, perm[301];
  char temp[301];
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> t;
    x[t] = i;
  }
  for (i = 1; i <= n; i++) {
    cin >> temp;
    for (j = 0; j < n; j++) v[i][j + 1] = temp[j] - '0';
  }
  for (i = 1; i <= n; i++) {
    p = i;
    Go(i);
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (v1[x[i]][j] == 1 && v2[j] == 0) {
        v2[j] = 1;
        perm[j] = i;
        break;
      }
    }
  }
  for (i = 1; i <= n; i++) cout << perm[i] << " ";
  return 0;
}
