#include <bits/stdc++.h>
using namespace std;
vector<int> reach[301];
vector<int> adj[301];
int vis[301];
void dfs(int ver, int start) {
  vis[ver] = 1;
  reach[start].push_back(ver);
  int cur;
  for (int x = 0; x != adj[ver].size(); x++) {
    cur = adj[ver][x];
    if (!vis[cur]) dfs(cur, start);
  }
}
int main() {
  int n, i, j;
  cin >> n;
  int p[n];
  for (i = 0; i < n; i++) {
    cin >> p[i];
  }
  string A[n];
  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (A[i][j] == '1') {
        adj[i + 1].push_back(j + 1);
      }
    }
  }
  vector<int> temp;
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, i);
      temp.clear();
      for (j = 0; j != reach[i].size(); j++) {
        temp.push_back(p[reach[i][j] - 1]);
      }
      sort(temp.begin(), temp.end());
      sort(reach[i].begin(), reach[i].end());
      for (j = 0; j != reach[i].size(); j++) {
        p[reach[i][j] - 1] = temp[j];
      }
    }
  }
  for (i = 0; i < n; i++) {
    cout << p[i] << " ";
  }
}
