#include <bits/stdc++.h>
using std::getchar;
using std::printf;
using std::scanf;
const int MAXN = 300 + 1, INF = 1e9;
bool used[MAXN];
bool arrive[MAXN][MAXN];
int source[MAXN], ans[MAXN], n;
inline void dfs(int now, int root) {
  for (int i = 1; i <= n; i++)
    if (i != root && (root == now || !arrive[root][i]) && arrive[now][i]) {
      arrive[root][i] = arrive[i][root] = 1;
      dfs(i, root);
    }
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &source[i]);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= n; j++) arrive[i][j] = getchar() - '0';
  }
  for (int i = 1; i <= n; i++) dfs(i, i);
  for (int i = 1; i <= n; i++) {
    int choice = INF;
    for (int j = 1; j <= n; j++)
      if ((arrive[i][j] || i == j) && !used[source[j]] && source[j] < choice)
        choice = source[j];
    ans[i] = choice;
    used[choice] = 1;
  }
  for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}
