#include <bits/stdc++.h>
using namespace std;
int n;
char str[305][305];
int a[305];
int vis[305];
int tmp[305];
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  for (int i = 0; i < n; i++)
    if (str[x][i] == '1') dfs(i);
}
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    for (int i = 0; i < n; i++) scanf("%s", str[i]);
    for (int i = 0; i < n; i++)
      if (!vis[i]) {
        dfs(i);
        int k = 0;
        for (int j = 0; j < n; j++)
          if (vis[j]) tmp[k++] = a[j];
        sort(tmp, tmp + k);
        k = 0;
        for (int j = 0; j < n; j++)
          if (vis[j]) a[j] = tmp[k++];
        memset(vis, 0, sizeof(vis));
      }
    for (int i = 0; i < n - 1; i++) printf("%d ", a[i]);
    printf("%d\n", a[n - 1]);
  }
  return 0;
}
