#include <bits/stdc++.h>
using namespace std;
int chh;
const int N = 305;
int n;
int a[N], g[N][N], b[N];
int main() {
  int id, t, x, y;
  char ch;
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[a[i]] = i;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        ch = getchar();
        while (ch != '0' && ch != '1') ch = getchar();
        g[i][j] = (ch == '1');
      }
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          if (i == j || i == k || j == k) continue;
          g[i][j] |= g[i][k] & g[k][j];
        }
    }
    for (int i = 1; i <= n; i++) {
      id = b[i], t = -1;
      for (int j = 1; j <= n; j++) {
        if (!g[id][j]) continue;
        if (a[j] < i) continue;
        if (j >= id) break;
        t = j;
        break;
      }
      if (t == -1) continue;
      x = a[id], y = a[t];
      swap(a[id], a[t]);
      swap(b[x], b[y]);
    }
    for (int i = 1; i <= n; i++) printf("%d ", a[i]);
    puts("");
  }
  return 0;
}
