#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
int n;
char matrix[MAXN][MAXN];
bool used[MAXN];
vector<vector<int> > component;
void dfs(int v) {
  used[v] = true;
  component[component.size() - 1].push_back(v);
  for (int to = 0; to < n; to++) {
    if (matrix[v][to] == '1' && !used[to]) {
      dfs(to);
    }
  }
}
int main() {
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> matrix[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      component.push_back(vector<int>());
      dfs(i);
    }
  }
  for (int i = 0; i < component.size(); i++) {
    sort(component[i].begin(), component[i].end());
  }
  vector<int> ans(n);
  for (int i = 0; i < component.size(); i++) {
    vector<int> m;
    vector<int> index;
    for (int to : component[i]) {
      m.push_back(p[to]);
      index.push_back(to);
    }
    sort(m.begin(), m.end());
    for (int j = 0; j < m.size(); j++) {
      ans[index[j]] = m[j];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
