#include <bits/stdc++.h>
using namespace std;
int par[301];
int root(int v) { return par[v] < 0 ? v : (par[v] = root(par[v])); }
void Union(int u, int v) {
  if ((u = root(u)) == (v = root(v))) return;
  if (par[u] < par[v]) {
    par[u] += par[v];
    par[v] = u;
  } else {
    par[v] += par[u];
    par[u] = v;
  }
}
int main() {
  int n;
  bool Free[301];
  char a[301][301];
  int p[301];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    par[i] = -1;
    Free[i] = true;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      if (a[i][j] == '1') Union(p[i], p[j]);
    }
  for (int i = 1; i <= n; i++) {
    int min = p[i], u = 0;
    for (int j = 1; j <= n; j++)
      if (i < j)
        if (root(p[j]) == root(p[i]) && (p[j] < min) && Free[p[j]]) {
          min = p[j];
          u = j;
        }
    if (u != 0) {
      Free[p[u]] = false;
      swap(p[i], p[u]);
    }
  }
  for (int i = 1; i <= n; i++) cout << p[i] << " ";
}
