#include <bits/stdc++.h>
using namespace std;
const int base = 1000100000;
const int base_digits = 9;
long long power(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b % 2 == 1) ret = (ret * a) % (int)(1e9 + 7);
    a = (a * a) % (int)(1e9 + 7);
    b /= 2;
  }
  return ret;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int perm[305];
char matrix[305][305];
bool visited[305];
int ans[305];
vector<int> adj[305];
vector<int> indexes;
vector<int> a;
void dfs(int cur) {
  visited[cur] = 1;
  indexes.push_back(cur);
  a.push_back(perm[cur]);
  for (int i = 0; i < adj[cur].size(); i++) {
    if (visited[adj[cur][i]] == 0) dfs(adj[cur][i]);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> perm[i];
  }
  for (int i = 0; i < n; i++) scanf("%s", matrix[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (matrix[i][j] == '1') {
        adj[i].push_back(j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      indexes.clear();
      a.clear();
      dfs(i);
      sort(a.begin(), a.end());
      sort(indexes.begin(), indexes.end());
      for (int i = 0; i < a.size(); i++) {
        ans[indexes[i]] = a[i];
      }
    }
  }
  for (int i = 0; i < n; i++) printf("%d ", ans[i]);
  return 0;
}
