#include <bits/stdc++.h>
using namespace std;
int INF = 1e9;
double eps = 1e-9;
bool a[500][500] = {0};
int p[400];
class node {
 public:
  vector<int> near;
};
node b[500];
vector<int> tmp;
bool df[500] = {0};
void dfs(int j) {
  df[j] = 1;
  vector<int> v = b[j].near;
  tmp.push_back(j);
  for (int i = 0; i < v.size(); i++) {
    if (!df[v[i]]) dfs(v[i]);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> p[i];
  string s;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    for (int j = 1; j <= n; j++) a[i][j] = (s[j - 1] == '1');
    for (int k = 1; k <= n; k++)
      if (a[i][k]) b[i].near.push_back(k);
  }
  for (int i = 1; i <= n; i++) {
    if (df[i]) continue;
    tmp.clear();
    dfs(i);
    vector<int> res;
    for (int i = 0; i < tmp.size(); i++) res.push_back(p[tmp[i]]);
    sort(res.begin(), res.end());
    sort(tmp.begin(), tmp.end());
    for (int i = 0; i < tmp.size(); i++) p[tmp[i]] = res[i];
  }
  for (int i = 1; i <= n; i++) cout << p[i] << " ";
  cout << endl;
}
