User ainta has a permutation *p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">2</sub>, ..., *p*<sub class="lower-index">*n*</sub>. As the New Year is coming, he wants to make his permutation as pretty as possible.

Permutation *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*n*</sub> is prettier than permutation *b*<sub class="lower-index">1</sub>, *b*<sub class="lower-index">2</sub>, ..., *b*<sub class="lower-index">*n*</sub>, if and only if there exists an integer *k* (1 ≤ *k* ≤ *n*) where *a*<sub class="lower-index">1</sub> = *b*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub> = *b*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*k* - 1</sub> = *b*<sub class="lower-index">*k* - 1</sub> and *a*<sub class="lower-index">*k*</sub> &lt; *b*<sub class="lower-index">*k*</sub> all holds.

As known, permutation *p* is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an *n* × *n* binary matrix *A*, user ainta can swap the values of *p*<sub class="lower-index">*i*</sub> and *p*<sub class="lower-index">*j*</sub> (1 ≤ *i*, *j* ≤ *n*, *i* ≠ *j*) if and only if *A*<sub class="lower-index">*i*, *j*</sub> = 1.

Given the permutation *p* and the matrix *A*, user ainta wants to know the prettiest permutation that he can obtain.
The first line contains an integer *n* (1 ≤ *n* ≤ 300) — the size of the permutation *p*.

The second line contains *n* space-separated integers *p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">2</sub>, ..., *p*<sub class="lower-index">*n*</sub> — the permutation *p* that user ainta has. Each integer between 1 and *n* occurs exactly once in the given permutation.

Next *n* lines describe the matrix *A*. The *i*-th line contains *n* characters '0' or '1' and describes the *i*-th row of *A*. The *j*-th character of the *i*-th line *A*<sub class="lower-index">*i*, *j*</sub> is the element on the intersection of the *i*-th row and the *j*-th column of A. It is guaranteed that, for all integers *i*, *j* where 1 ≤ *i* &lt; *j* ≤ *n*, *A*<sub class="lower-index">*i*, *j*</sub> = *A*<sub class="lower-index">*j*, *i*</sub> holds. Also, for all integers *i* where 1 ≤ *i* ≤ *n*, *A*<sub class="lower-index">*i*, *i*</sub> = 0 holds.
# import string
# from sys import setrecursionlimit
# setrecursionlimit(300000)

# def lcs(a, b, m, n):
#     if m==0 or n==0:
#         return 0
#     if record[m][n] != -1:
#         return record[m][n]
#     if a[m-1] == b[n-1]:
#         record[m][n] = lcs(a, b, m-1, n-1) + 1
#         return record[m][n]
#     else:
#         record[m][n] = max(lcs(a, b, m-1, n), lcs(a, b, m, n-1))
#         return record[m][n]


# n1 = int(input())
# a = list(map(int, input().split()))
# n2 = int(input())
# b = list(map(int, input().split()))
# record = [ [-1 for i in range(n2+1)] for j in range(n1+1)]
# print(lcs(a, b, n1, n2))

#print("YNEOS"[((n%2<1 or d+n>t) if d%2 else d>t)::2])

#open(0) is sys.stdin 
#So [*open(0)] is telling python to put every line of stdin into a list.
#(s,n),*a=[[*map(int,z.split())]for z in open(0)]


def dfs(v):
    pos.append(v)
    used[v] = True
    for g in range(n):
        if int(s[v][g]) and not used[g]:
            dfs(g)
 
n = int(input())
p = list(map(int, input().split()))
s = [input() for i in range(n)]
used = [False] * n
for j in range(n):
    if not used[j]:
        pos = []
        dfs(j)
        values = [p[i] for i in pos]
        pos.sort()
        values.sort()
        for (i, po) in enumerate(pos):
            p[po] = values[i]
print(' '.join([str(x) for x in p]))