#include <bits/stdc++.h>
using namespace std;
bool v[1234];
int b[1234], a[1234];
string s[1234];
vector<int> pos;
vector<int> num;
void dfs(int current) {
  for (size_t i = 0; i < s[current].length(); i++) {
    if (s[current][i] == '1') {
      if (!v[i]) {
        v[i] = true;
        dfs(i);
      }
    }
  }
  pos.push_back(current);
  num.push_back(a[current]);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (!v[i]) {
      v[i] = true;
      dfs(i);
      sort(pos.begin(), pos.end());
      sort(num.begin(), num.end());
      for (size_t j = 0; j < pos.size(); j++) {
        b[pos[j]] = num[j];
      }
      pos.clear();
      num.clear();
    }
  }
  for (int i = 0; i < n; i++) cout << b[i] << " ";
}
