#include <bits/stdc++.h>
using namespace std;
int main() {
  char A[301][301];
  int num[301];
  int n, i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> num[i];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) cin >> A[i][j];
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        if (A[i][k] == '1' && A[k][j] == '1') A[i][j] = '1';
  for (i = 1; i <= n; i++)
    for (j = i + 1; j <= n; j++)
      if (A[i][j] == '1' && num[j] < num[i]) swap(num[j], num[i]);
  for (i = 1; i <= n; i++) cout << num[i] << " ";
  return 0;
}
