#include <bits/stdc++.h>
using namespace std;
const int N = 305;
int p[N], n, ans[N];
bool dis[N][N];
string mtx[N];
void Read() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
  }
  for (int i = 1; i <= n; i++) {
    cin >> mtx[i];
  }
}
void Prefix() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = mtx[i][j - 1] - '0';
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dis[i][j] |= dis[i][k] & dis[k][j];
      }
    }
  }
}
void Solve(int step) {
  if (step == n + 1) {
    return;
  }
  int k;
  for (int i = 1; i <= n; i++) {
    if (p[i] == step) {
      k = i;
      break;
    }
  }
  for (int i = 1; i <= k; i++) {
    if (!ans[i] && (i == k || dis[i][k])) {
      ans[i] = step;
      swap(p[i], p[k]);
      Solve(step + 1);
      swap(p[i], p[k]);
    }
  }
}
int main() {
  Read();
  Prefix();
  Solve(1);
  for (int i = 1; i <= n; i++) {
    printf("%d ", ans[i]);
  }
  return 0;
}
