#include <bits/stdc++.h>
using namespace std;
long long int digits(long long int n) { return floor(log10(double(n))) + 1; }
void faster() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
long long int modularExponentiation(long long int x, long long int n,
                                    long long int M) {
  long long int result = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      result = ((result % M) * (x % M)) % M;
    }
    x = ((x % M) * (x % M)) % M;
    n = n / 2;
  }
  return result % M;
}
bool adj[301][301];
bool vis[301];
vector<long long int> temp;
long long int n;
void dfs(long long int x) {
  vis[x] = true;
  temp.push_back(x);
  for (int i = 0; i < n; i++) {
    if (!vis[i] && adj[x][i]) {
      dfs(i);
    }
  }
}
int main() {
  faster();
  cin >> n;
  long long int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i], arr[i]--;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '1')
        adj[i][j] = 1;
      else
        adj[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) vis[i] = false;
  vector<long long int> ans(n);
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      temp.clear();
      dfs(i);
      vector<long long int> store;
      for (int j = 0; j < temp.size(); j++) {
        store.push_back(arr[temp[j]]);
      }
      sort(temp.begin(), temp.end());
      sort(store.begin(), store.end());
      for (int j = 0; j < temp.size(); j++) {
        ans[temp[j]] = store[j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    ans[i]++;
    cout << ans[i] << " ";
  }
}
