#include <bits/stdc++.h>
long long int mod = (long long int)1000000007;
using namespace std;
int n;
const int full = 1000001;
vector<int> adj[full];
bool vis[full];
int par[full];
int depth[full];
int p[full];
int a[full];
vector<int> path[full];
int curr;
void dfs(int i) {
  vis[i] = 1;
  path[curr].push_back(i);
  for (int j = 0; j < adj[i].size(); j++) {
    if (vis[adj[i][j]] == 0) {
      dfs(adj[i][j]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int i, j, k;
  int n, m;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      char g;
      cin >> g;
      if (g == '1') {
        adj[i].push_back(j);
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      curr = i;
      dfs(i);
    }
  }
  vector<int> x[n + 1];
  for (i = 1; i <= n; i++) {
    for (j = 0; j < path[i].size(); j++) {
      x[i].push_back(p[path[i][j] - 1]);
    }
    sort(x[i].begin(), x[i].end());
    sort(path[i].begin(), path[i].end());
  }
  for (i = 1; i <= n; i++) {
    for (j = 0; j < path[i].size(); j++) {
      if (a[path[i][j] - 1] == 0) {
        a[path[i][j] - 1] = x[i][j];
      }
    }
  }
  for (i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
}
