#include <bits/stdc++.h>
using namespace std;
int ar[310];
bool mp[310][310];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> ar[i];
  string str;
  for (int i = 1; i <= n; i++) {
    cin >> str;
    for (int j = 0; j < n; j++)
      if (str[j] == '1') mp[i][j + 1] = true;
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (mp[i][k] && mp[k][j]) mp[i][j] = true;
  for (int i = 1; i < n; i++)
    for (int j = i + 1; j <= n; j++)
      if (ar[i] > ar[j] && mp[i][j]) swap(ar[i], ar[j]);
  cout << ar[1];
  for (int i = 2; i <= n; i++) cout << " " << ar[i];
  cout << "\n";
}
