#include <bits/stdc++.h>
using namespace std;
vector<int> edges[1000];
bool visited[1000];
int arr[1000];
vector<vector<int>> output;
vector<int> component;
void dfs(int start) {
  visited[start] = true;
  component.push_back(start);
  for (int i = 0; i < edges[start].size(); i++) {
    int neighbor = edges[start][i];
    if (!visited[neighbor]) {
      dfs(neighbor);
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < n; j++) {
      if (str[j] == '1') {
        edges[i].push_back(j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
      vector<int> s(component.size());
      for (int i = 0; i < s.size(); i++) {
        s[i] = component[i];
      }
      output.push_back(s);
      component.clear();
    }
  }
  for (int i = 0; i < output.size(); i++) {
    vector<int> value;
    vector<int> index;
    for (int j = 0; j < output[i].size(); j++) {
      int idx = output[i][j];
      index.push_back(idx);
      value.push_back(arr[idx]);
    }
    sort(value.begin(), value.end());
    sort(index.begin(), index.end());
    for (int j = 0; j < index.size(); j++) {
      arr[index[j]] = value[j];
    }
    index.clear();
    value.clear();
  }
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
}
