#include <bits/stdc++.h>
using namespace std;
struct debugger {
  template <typename T>
  debugger& operator,(const T v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
int n;
char vec[309][309];
int num[309], pos[309];
int color[309], occ[309];
int nxt, ans;
void dfs(int u) {
  if (color[u]) return;
  color[u] = 1;
  if (u == nxt) ans = true;
  for (int i = 0; i < n; i++) {
    if (vec[u][i] == '1') {
      dfs(i);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int i, j, k;
  while (cin >> n) {
    for (i = 0; i < (n); i++) {
      cin >> pos[i];
    }
    for (i = 0; i < (n); i++) cin >> vec[i];
    for (i = 1; i <= n; i++) {
      for (j = 0; j < n; j++) {
        if (pos[j] == i) {
          break;
        }
      }
      for (k = 0; k < n; k++) {
        if (occ[k]) continue;
        nxt = k;
        ans = false;
        memset(color, 0, sizeof(color));
        dfs(j);
        if (ans) {
          swap(pos[k], pos[j]);
          occ[k] = 1;
          break;
        }
      }
    }
    for (i = 0; i < (n); i++) cout << pos[i] << " ";
    cout << endl;
  }
}
