#include <bits/stdc++.h>
using namespace std;
bool tempused[301];
int used[301];
char matrix[301][301];
int place[301], minx, n;
void dfs(int v) {
  if (tempused[v]) {
    return;
  }
  tempused[v] = true;
  if (!used[v]) {
    minx = min(minx, v);
  }
  for (int i = 0; i < n; i++) {
    if (matrix[v][i] == '1') {
      dfs(i);
    }
  }
}
int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    place[a - 1] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> matrix[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      tempused[j] = false;
    }
    minx = 1 << 30;
    dfs(place[i]);
    used[minx] = i + 1;
  }
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << used[i];
  }
  cout << "\n";
  return 0;
}
