#include <bits/stdc++.h>
using namespace std;
const long long int dx[4] = {+1, -1, 0, 0};
const long long int dy[4] = {0, 0, +1, -1};
const long long int MOD = 1e9 + 7;
const long long int INF = 1e10;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  vector<long long int> p(n);
  vector<string> a(n);
  for (long long int i = 0; i < n; i++) cin >> p[i];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < n; j++) {
      a[i][j] -= '0';
    }
  }
  for (long long int k = 0; k < n; k++) {
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {
        a[i][j] |= a[i][k] & a[k][j];
      }
    }
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = i + 1; j < n; j++) {
      if (a[i][j] && p[j] < p[i]) swap(p[i], p[j]);
    }
  }
  for (auto x : p) cout << x << " ";
  return 0;
}
