#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18;
const long long MOD = (long long)1e9 + 7;
int n, r[400], m[400][400], mn, i, j, k;
char c;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%d", &r[i]);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j) {
      cin >> c;
      m[i][j] = c - '0';
    }
  for (k = 1; k <= n; ++k)
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= n; ++j) m[i][j] |= m[i][k] && m[k][j];
  for (k = 1; k <= n; ++k) {
    mn = k;
    for (i = k + 1; i <= n; ++i)
      if (m[k][i] && r[i] < r[mn]) mn = i;
    swap(r[k], r[mn]);
  }
  for (i = 1; i <= n; ++i) printf("%d ", r[i]);
}
