#include <bits/stdc++.h>
using namespace std;
int a[301], b[301];
char c[301][301];
set<int> second[301];
set<int>::iterator it;
void init(int n) {
  for (int i = 1; i <= n; ++i) {
    a[i] = i;
    second[i].insert(b[i]);
  }
}
int root(int i) {
  if (i == a[i]) return i;
  return root(a[i]);
}
void unn(int p, int q) {
  p = root(p);
  q = root(q);
  if (p == q) return;
  a[q] = a[p];
  for (it = second[q].begin(); it != second[q].end(); ++it)
    second[p].insert(*it);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> b[i];
  init(n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) cin >> c[i][j];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (c[i][j] == '1') unn(i, j);
  for (int i = 1; i <= n; ++i) {
    int x = root(i);
    if (second[x].empty()) {
      cout << "WTF IS HAPPENING";
      return 0;
    }
    int y = *second[x].begin();
    cout << y << " ";
    second[x].erase(y);
  }
  return 0;
}
