#include <bits/stdc++.h>
using namespace std;
int const MAX = 1e6;
int cur_min, cur_ind;
vector<int> adj[301];
int vis[301];
int p[300];
int c;
void dfs(int k) {
  if (vis[k]) return;
  vis[k] = 1;
  if (p[k] < cur_min && k >= c) cur_min = p[k], cur_ind = k;
  for (auto x : adj[k]) dfs(x);
}
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> p[i];
  string matrix[n];
  for (int i = 0; i < n; i++) cin >> matrix[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (matrix[i][j] - '0') {
        adj[i].push_back(j);
      }
    }
  }
  for (c = 0; c < n; c++) {
    memset(vis, 0, sizeof(vis));
    cur_min = p[c];
    cur_ind = c;
    dfs(c);
    swap(p[c], p[cur_ind]);
  }
  for (int i = 0; i < n; i++) cout << p[i] << " ";
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
