#include <bits/stdc++.h>
using namespace std;
using namespace std;
int a[400][400];
vector<int> b[400];
int c[400];
int cc[400];
int shomar[400];
int n, m, k, ans = 0;
bool mark[400];
void dfs(int v, int sh) {
  b[sh].push_back(c[v]);
  cc[v] = sh;
  mark[c[v]] = true;
  for (int i = 0; i < n; i++) {
    int u = a[v][i];
    if (u == 1) {
      if ((!mark[c[i]])) {
        dfs(i, sh);
      }
    }
  }
}
int main() {
  cin >> n;
  int sho = 0;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') a[i][j] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!mark[c[i]]) dfs(i, sho++);
  }
  for (int i = 0; i < sho; i++) {
    sort(b[i].begin(), b[i].end());
  }
  for (int i = 0; i < n; i++) {
    cout << b[cc[i]][shomar[cc[i]]++] + 1 << " ";
  }
}
