#include <bits/stdc++.h>
using namespace std;
unsigned char G[301][301];
unsigned char bv[301];
int ints[301];
int main() {
  int n;
  cin >> n;
  for (register int i = 1; i <= n; i++) cin >> ints[i];
  for (register int i = 1; i <= n; i++) {
    for (register int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      G[i][j] = c - '0';
    }
  }
  for (register int k = 1; k <= n; k++) {
    for (register int i = 1; i <= n; i++) {
      for (register int j = 1; j <= n; j++) {
        G[i][j] = G[i][j] || (G[i][k] && G[k][j]);
      }
    }
  }
  for (register int i = 1; i <= n; i++) {
    G[i][i] = 1;
  }
  ints[0] = 0x7fffffff;
  for (register int i = 1; i <= n; i++) {
    int minn = 0;
    for (register int j = 1; j <= n; j++) {
      if (ints[j] < ints[minn] && G[i][j] && bv[j] == 0) minn = j;
    }
    printf("%d ", ints[minn]);
    bv[minn] = 1;
  }
  return (0);
}
