#include <bits/stdc++.h>
using namespace std;
int n, ara[305], pos[305];
vector<int> g[305], N, POS;
int ans[305];
int vis[305];
void solve() {
  sort(N.begin(), N.end());
  sort(POS.begin(), POS.end());
  for (int i = 0; i < POS.size(); i++) {
    ans[POS[i]] = N[i];
  }
}
void dfs(int u) {
  vis[u] = 1;
  POS.push_back(u);
  N.push_back(ara[u]);
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (vis[v] == 0) dfs(v);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> ara[i + 1];
    pos[ara[i + 1]] = i + 1;
  }
  for (int i = 1; i <= n; i++) {
    string mat;
    cin >> mat;
    for (int j = 0; j < n; j++) {
      if (mat[j] == '1') g[i].push_back(j + 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      POS.clear();
      N.clear();
      dfs(i);
      solve();
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
