#include <bits/stdc++.h>
using namespace std;
vector<long long int> v;
vector<vector<long long int> > v1(500);
string s[300];
long long int dsu[2000005], count1 = 0;
void ds_start(long long int n) {
  long long int i, j, k;
  for (i = 0; i <= n; i++) dsu[i] = -1;
}
long long int parent(long long int n) {
  if (dsu[n] < 0) return n;
  long long int b = parent(dsu[n]);
  if (n != b) {
    dsu[n] = b;
  }
  return b;
}
bool dis_find(long long int x, long long int y) {
  long long int p_x = parent(x);
  long long int p_y = parent(y);
  return (p_x == p_y);
}
long long int dsw(long long int x) { return -1 * dsu[x]; }
long long int ds_union(long long int x, long long int y) {
  long long int p_x = parent(x);
  long long int p_y = parent(y);
  if (p_x != p_y) {
    if (dsw(p_x) >= dsw(p_y)) {
      dsu[p_x] += dsu[p_y];
      dsu[p_y] = p_x;
      return dsw(p_x);
    } else {
      dsu[p_y] += dsu[p_x];
      dsu[p_x] = p_y;
      return dsw(p_y);
    }
  } else {
    return -1;
  }
}
void ds_show(long long int n) {
  long long int i;
  for (i = 1; i <= n; i++) count1 = max(count1, parent(i));
}
int main() {
  long long int n, n1, m, i, j, k, p, q, r;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> j;
    v.push_back(j);
  }
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  ds_start(500);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (s[i][j] == '1') {
        ds_union(v[i], v[j]);
      }
    }
  }
  ds_show(n);
  for (i = 0; i < n; i++) {
    v1[parent(v[i])].push_back(v[i]);
  }
  for (i = 0; i < n; i++) {
    sort(v1[parent(v[i])].begin(), v1[parent(v[i])].end());
  }
  map<long long int, long long int> m2;
  for (i = 0; i < n; i++) {
    cout << v1[parent(v[i])][m2[parent(v[i])]] << " ";
    m2[parent(v[i])]++;
  }
  return 0;
}
