#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> V(n);
  for (int i = 0; i < n; ++i) {
    cin >> V[i];
    V[i]--;
  }
  vector<vector<int> > MAP(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      char c;
      cin >> c;
      MAP[i][j] = c - '0';
    }
  }
  vector<int> ans(n), taken(n);
  for (int i = 0; i < n; ++i) {
    vector<int> vis(n);
    queue<int> Q;
    Q.push(i);
    int ind = 1e6, mn = 1e6;
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      if (vis[v]) continue;
      if (!taken[V[v]]) {
        if (mn > V[v]) {
          mn = V[v];
          ind = v;
        }
      }
      vis[v] = 1;
      for (int j = 0; j < n; ++j) {
        if (MAP[v][j]) Q.push(j);
      }
    }
    taken[mn] = 1;
    ans[i] = mn + 1;
    V[ind] = V[i];
    V[i] = mn;
  }
  for (int i = 0; i < n; ++i) {
    if (i != 0) cout << " ";
    cout << ans[i];
  }
  cout << "\n";
  return 0;
}
