#include <bits/stdc++.h>
using namespace std;
int n, p[304], res[304];
bool a[304][304];
void process() {
  int i, j, k;
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++)
      if (i != k)
        if (a[i][k] == true) {
          for (j = 1; j <= n; j++)
            if (a[k][j] == true) a[i][j] = 1;
        }
  for (i = 1; i <= n; i++) res[i] = p[i];
  for (i = 1; i <= n; i++) {
    k = i;
    for (j = i + 1; j <= n; j++)
      if (a[i][j] == true) {
        if (res[j] < res[k]) k = j;
      }
    j = res[k];
    res[k] = res[i];
    res[i] = j;
  }
}
int main() {
  ios::sync_with_stdio(false);
  int i, j;
  string s;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> p[i];
  memset(a, 0, sizeof(a));
  for (i = 1; i <= n; i++) {
    cin >> s;
    for (j = 1; j <= n; j++) a[i][j] = (s[j - 1] == '1');
  }
  process();
  for (i = 1; i <= n; i++) cout << res[i] << " ";
  return 0;
}
