#include <bits/stdc++.h>
using namespace std;
vector<int> g[301];
pair<int, int> a[301];
bool matrix[301][301] = {0};
int assign[301];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, i, j;
  cin >> n;
  for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) {
    cin >> a[i].first;
    a[i].second = i;
  }
  for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) {
    char x[301] = {NULL};
    cin >> x;
    for (j = 0; 0 < n ? j < n : j > n; 0 < n ? j += 1 : j -= 1) {
      if (x[j] == '1') {
        g[i].push_back(j + 1);
      }
    }
  }
  for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) {
    queue<int> q;
    q.push(i);
    bool visited[301] = {0};
    visited[i] = 1;
    while (!q.empty()) {
      int node = q.front();
      q.pop();
      matrix[i][node] = 1;
      int x = g[node].size();
      while (x--) {
        int next = g[node][x];
        if (!visited[next]) {
          visited[next] = 1;
          q.push(next);
        }
      }
    }
  }
  sort(a + 1, a + n + 1);
  for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) {
    int cur = a[i].first;
    int index = a[i].second;
    for (j = 1; 1 < n + 1 ? j < n + 1 : j > n + 1;
         1 < n + 1 ? j += 1 : j -= 1) {
      if (matrix[index][j] == 1 && assign[j] == 0) {
        assign[j] = cur;
        break;
      }
    }
  }
  for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) {
    cout << assign[i] << " ";
  }
  return 0;
}
