#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
using vl = vector<ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
const ll N = 1e6 + 5;
vector<ll> adj[N];
ll p[N];
vl comp[N];
ll num = 0;
bool vis[N];
ll col[N];
void dfs(ll u) {
  vis[u] = true;
  comp[num].push_back(u);
  col[p[u]] = num;
  for (auto& v : adj[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}
bool cmp(ll i, ll j) { return p[i] < p[j]; }
void solve() {
  ll n;
  cin >> n;
  for (ll i = (1); i < (n + 1); ++i) {
    cin >> p[i];
  }
  char grid[n + 1][n + 1];
  for (ll i = (1); i < (n + 1); ++i) {
    for (ll j = (1); j < (n + 1); ++j) {
      cin >> grid[i][j];
    }
  }
  for (ll i = (1); i < (n + 1); ++i) {
    for (ll j = (1); j < (n + 1); ++j) {
      if (grid[i][j] == '1') {
        adj[i].push_back(j);
      }
    }
  }
  for (ll i = (1); i < (n + 1); ++i) {
    if (!vis[i]) {
      num++;
      dfs(i);
    }
  }
  for (ll i = 1; i <= n; i++) {
    ll idx = -1;
    ll coll = INF;
    for (ll j = (i + 1); j < (n + 1); ++j) {
      if (col[p[i]] == col[p[j]] && p[i] > p[j] && p[j] < coll) {
        coll = p[j];
        idx = j;
      }
    }
    if (idx != -1) {
      swap(p[i], p[idx]);
    }
  }
  for (ll i = (1); i < (n + 1); ++i) cout << p[i] << " ";
  cout << endl;
}
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  ll t = 1;
  while (t--) {
    solve();
  }
}
