#include <bits/stdc++.h>
using namespace std;
vector<int> g[301];
vector<set<int> > components;
bool visited[301] = {0};
void bfs(int node) {
  set<int> currentComponent;
  queue<int> q;
  q.push(node);
  while (!q.empty()) {
    for (int i = 0; i < g[q.front()].size(); i++) {
      int currentNode = g[q.front()][i];
      if (!visited[currentNode]) {
        visited[currentNode] = 1;
        q.push(currentNode);
      }
    }
    visited[q.front()] = 1;
    currentComponent.insert(q.front());
    q.pop();
  }
  if (currentComponent.size() > 1) components.push_back(currentComponent);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  char b;
  cin >> n;
  int* input = new int[n];
  for (int i = 0; i < n; i++) cin >> input[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> b;
      if (b == '1') {
        g[i].push_back(j);
      }
    }
  for (int i = 0; i < n; i++)
    if (!visited[i]) bfs(i);
  int k;
  set<int>::iterator j;
  for (int i = 0; i < components.size(); i++) {
    vector<int> s;
    for (j = components[i].begin(), k = 0; j != components[i].end(); j++, k++)
      s.push_back(input[*j]);
    sort(s.begin(), s.end());
    for (j = components[i].begin(), k = 0; j != components[i].end(); j++, k++) {
      input[*j] = s[k];
    }
  }
  for (int i = 0; i < n; i++) cout << input[i] << " ";
  cout << endl;
  return 0;
}
