#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 3 * 100 + 5;
int ar[MAXN], pos[MAXN], ans[MAXN];
bool vis[MAXN];
char s[MAXN][MAXN];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &ar[i]);
      pos[ar[i]] = i;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%s", s[i] + 1);
      s[i][i] = '1';
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          if (s[j][i] == '1' && s[k][i] == '1') {
            s[j][k] = '1';
          }
        }
      }
    }
    memset(vis, false, sizeof(vis));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (!vis[j] && s[i][pos[j]] == '1') {
          ans[i] = j;
          vis[j] = 1;
          pos[j] = i;
          pos[ar[i]] = pos[j];
          break;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (i != n)
        printf("%d ", ans[i]);
      else
        printf("%d\n", ans[i]);
    }
  }
  return 0;
}
