#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int n;
int a[333], used[333];
char p[333][333];
void dfs(int x) {
  used[x] = true;
  for (int i = 1; i <= n; i++)
    if (!used[i] && p[x][i] == '1') dfs(i);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> p[i][j];
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= n; k++) used[k] = false;
    dfs(i);
    int minl = INF, ind = i;
    for (int j = i + 1; j <= n; j++) {
      if (used[j] && a[j] < a[i]) {
        if (a[j] < minl) minl = a[j], ind = j;
      }
    }
    swap(a[i], a[ind]);
  }
  for (int i = 1; i <= n; i++) printf("%d ", a[i]);
  return 0;
}
