#include <bits/stdc++.h>
std::vector<int> v[333];
char F[333][333];
int P[333];
int temp[333];
int Fix[333];
int mini;
void go(int, int*);
int main() {
  int n, i, j, k, temp, visit[333];
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &P[i]);
  for (i = 1; i <= n; i++) scanf("%s", F[i] + 1);
  for (i = 1; i <= n; i++)
    for (j = 1; j < i; j++) {
      if (F[i][j] == '1') {
        v[i].push_back(j);
        v[j].push_back(i);
      }
    }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) visit[j] = 0;
    for (j = 1; j <= n; j++)
      if (P[j] == i) break;
    k = j;
    mini = k;
    go(k, visit);
    Fix[mini] = i;
    temp = P[j];
    P[j] = P[mini];
    P[mini] = temp;
  }
  for (i = 1; i <= n; i++) printf("%d ", Fix[i]);
  return 0;
}
void go(int k, int* visit) {
  int i;
  visit[k]++;
  for (i = 0; i < v[k].size(); i++) {
    if (visit[v[k][i]] == 0) go(v[k][i], visit);
    if (mini > v[k][i] && Fix[v[k][i]] == 0) mini = v[k][i];
  }
  return;
}
