#include <bits/stdc++.h>
using namespace std;
const int N = 310;
int A[N][N];
int p[N], op[N];
int answer[N], a[N];
int n;
int marker[N];
set<int> SS;
void dfs(int i) {
  if (marker[i]) return;
  SS.insert(p[i]);
  marker[i] = 1;
  for (int j = 1; j <= n; j++) {
    if (A[i][j]) dfs(j);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i + 1]);
    op[p[i + 1]] = i + 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) scanf("%1d", &A[i][j]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        if (A[i][k] && A[k][j]) A[i][j] = 1;
      }
    }
  }
  set<int> t;
  for (int i = 1; i <= n; i++) {
    SS.clear();
    fill(marker, marker + n + 1, 0);
    dfs(i);
    set<int> S = SS;
    for (auto it = S.begin(); it != S.end(); it++) {
      if (t.find(*it) == t.end()) {
        a[i] = *it;
        t.insert(*it);
        break;
      }
    }
  }
  for (int i = 1; i <= n; i++) printf("%d ", a[i]);
  printf("\n");
}
