#include <bits/stdc++.h>
using namespace std;
vector<int> graph[40002];
int arr[10000], n;
char mat[333][333];
void dfs(int src, vector<int> &visited) {
  visited[src] = 1;
  for (int i = 0; i < graph[src].size(); i++) {
    int ver = graph[src][i];
    if (!visited[ver]) {
      dfs(ver, visited);
    }
  }
}
void dfs_loop() {
  for (int i = 1; i <= n; i++) {
    vector<int> visited(n + 2, 0);
    dfs(i, visited);
    for (int j = 1; j <= n; j++) {
      if (visited[j]) mat[i][j] = '1';
    }
    mat[i][i] = '1';
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> mat[i][j];
      if (mat[i][j] == '1') {
        graph[i].push_back(j);
        graph[j].push_back(i);
      }
    }
  }
  dfs_loop();
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (mat[i][j] == '1' && arr[i] > arr[j]) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
      }
    }
  }
  for (int i = 1; i <= n; i++) cout << arr[i] << " ";
  cout << "\n";
  return 0;
}
