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 gc
import itertools
import math
import sqlite3
from collections import Counter, deque, defaultdict
from sys import stdout
import time
from math import factorial, log, gcd
import sys
from decimal import Decimal
import threading
from heapq import *


def S():
    return sys.stdin.readline().split()


def I():
    return [int(i) for i in sys.stdin.readline().split()]


def II():
    return int(sys.stdin.readline())


def IS():
    return sys.stdin.readline().replace('\n', '')


def _next(c, a1, a2):
    if a1 > a2:
        a1, a2 = a2, a1
    if c + 1 != a1 and c + 1 != a2:
        return c + 1
    elif c + 2 != a2:
        return c + 2
    else:
        return c + 3


def main():
    n = II()
    p = I()
    idxing = [-1] * (n + 1)
    for i in range(n):
        idxing[p[i]] = i

    matrix = [IS() for _ in range(n)]
    indexes = set(range(n))
    while indexes:
        idx = indexes.pop()
        keys = [idx]
        values = [p[idx]]
        queue = deque([idx])
        while queue:
            idx = queue.pop()
            for i in range(n):
                if matrix[idx][i] == '1' and i in indexes:
                    queue.append(i)
                    indexes.remove(i)
                    values.append(p[i])
                    keys.append(i)
        values.sort()
        keys.sort()
        for i in range(len(keys)):
            p[keys[i]] = values[i]
    print(*p)



if __name__ == '__main__':
    # for _ in range(II()):
    #     main()
    main()