#include <bits/stdc++.h>
using namespace std;
long long n, pos[301], per[301], mat[301][301], fir[301][301], temat[301][301],
    temul[301][301];
void mul() {
  long long i, j, k;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      temat[i][j] = 0;
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      for (k = 0; k < n; ++k) {
        temat[i][j] += (fir[i][k] * mat[k][j]);
      }
      if (temat[i][j] > 0) temat[i][j] = 1;
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      fir[i][j] = temat[i][j];
    }
  }
}
void mulmat() {
  long long i, j, k;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      temul[i][j] = 0;
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      for (k = 0; k < n; ++k) {
        temul[i][j] += (mat[i][k] * mat[k][j]);
      }
      if (temul[i][j] > 0) temul[i][j] = 1;
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      mat[i][j] = temul[i][j];
    }
  }
}
void power() {
  long long p, i, j;
  p = n;
  while (p != 0) {
    if (p % 2 == 1) mul();
    mulmat();
    p /= 2;
  }
}
int main() {
  long long t, i, j, a, b, c, m, k;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> per[i];
    per[i]--;
    pos[per[i]] = i;
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      scanf("%1lld", &mat[i][j]);
      fir[i][j] = 0;
    }
    fir[i][i] = 1;
    mat[i][i] = 1;
  }
  for (k = 0; k < n; ++k)
    for (j = 0; j < n; ++j)
      for (i = 0; i < n; ++i) mat[i][j] |= (mat[i][k] * mat[k][j]);
  for (i = 0; i < n; ++i) {
    for (j = 0; j < pos[i]; ++j) {
      if ((mat[j][pos[i]] == 1) && (per[j] > per[pos[i]])) {
        pos[per[j]] = pos[i];
        per[pos[i]] = per[j];
        per[j] = i;
        break;
      }
    }
  }
  for (i = 0; i < n; ++i) cout << (per[i] + 1) << ' ';
  return 0;
}
