#include <bits/stdc++.h>
using namespace std;
const int N = 310;
int a[N][N], n, p[N], vis[N];
void dfs(int v) {
  vis[v] = 1;
  for (int i = 1; i <= n; i++) {
    if (a[v][i] && !vis[i]) {
      dfs(i);
    }
  }
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
  }
  scanf("\n");
  char ch;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      ch = getchar();
      if (ch == '0') {
        a[i][j] = 0;
      } else {
        a[i][j] = 1;
      }
    }
    scanf("\n");
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      vis[j] = 0;
    }
    dfs(i);
    int pos = -1;
    for (j = i; j <= n; j++) {
      if (vis[j]) {
        if ((pos == -1) || (p[pos] > p[j])) {
          pos = j;
        }
      }
    }
    swap(p[pos], p[i]);
  }
  for (i = 1; i <= n; i++) {
    printf("%d ", p[i]);
  }
  return 0;
}
