#include <bits/stdc++.h>
using namespace std;
char vis[305][305];
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  char c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> vis[i][j];
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (vis[i][k] == '1' && vis[k][j] == '1') vis[i][j] = '1';
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int in = i;
    for (int j = i + 1; j < n; j++) {
      if (v[in] > v[j] && vis[in][j] == '1') {
        in = j;
      }
    }
    swap(v[i], v[in]);
  }
  for (int i = 0; i < n; i++) cout << v[i] << " ";
  return 0;
}
