User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.

Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk 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 pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1.

Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.

Input

The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p.

The second line contains n space-separated integers p1, p2, ..., pn — 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 Ai, j 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 < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

Examples

Input

7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000


Output

1 2 4 3 6 7 5


Input

5
4 2 1 5 3
00100
00011
10010
01101
01010


Output

1 2 3 4 5

Note

In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).

In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). 

<image>

A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
	import java.io.*;
	import java.util.*;
	import java.math.*;
	
	public class Kruskal {
				private static InputStream stream;
				private static byte[] buf = new byte[1024];
				private static int curChar;
				private static int numChars;
				private static SpaceCharFilter filter;
				private static PrintWriter pw;
			
				public static void main(String args[]) throws Exception {
					InputReader(System.in);
					pw = new PrintWriter(System.out);
					//ans();
			       
			            soln();
			     
			        
					pw.close();
				}
				
			
				// To Get Input
				// Some Buffer Methods
			 
				public static void InputReader(InputStream stream1) {
					stream = stream1;
				}
			 
				private static boolean isWhitespace(int c) {
					return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
				}
			 
				private static boolean isEndOfLine(int c) {
					return c == '\n' || c == '\r' || c == -1;
				}
			 
				private static int read() {
					if (numChars == -1)
						throw new InputMismatchException();
					if (curChar >= numChars) {
						curChar = 0;
						try {
							numChars = stream.read(buf);
						} catch (IOException e) {
							throw new InputMismatchException();
						}
						if (numChars <= 0)
							return -1;
					}
					return buf[curChar++];
				}
			 
				private static int nextInt() {
					int c = read();
					while (isSpaceChar(c))
						c = read();
					int sgn = 1;
					if (c == '-') {
						sgn = -1;
						c = read();
					}
					int res = 0;
					do {
						if (c < '0' || c > '9')
							throw new InputMismatchException();
						res *= 10;
						res += c - '0';
						c = read();
					} while (!isSpaceChar(c));
					return res * sgn;
				}
			 
				private static long nextLong() {
					int c = read();
					while (isSpaceChar(c))
						c = read();
					int sgn = 1;
					if (c == '-') {
						sgn = -1;
						c = read();
					}
					long res = 0;
					do {
						if (c < '0' || c > '9')
							throw new InputMismatchException();
						res *= 10;
						res += c - '0';
						c = read();
					} while (!isSpaceChar(c));
					return res * sgn;
				}
			 
				private static String nextToken() {
					int c = read();
					while (isSpaceChar(c))
						c = read();
					StringBuilder res = new StringBuilder();
					do {
						res.appendCodePoint(c);
						c = read();
					} while (!isSpaceChar(c));
					return res.toString();
				}
			 
				private static String nextLine() {
					int c = read();
					while (isSpaceChar(c))
						c = read();
					StringBuilder res = new StringBuilder();
					do {
						res.appendCodePoint(c);
						c = read();
					} while (!isEndOfLine(c));
					return res.toString();
				}
			 
				private static int[] nextIntArray(int n) {
					int[] arr = new int[n];
					for (int i = 0; i < n; i++) {
						arr[i] = nextInt();
					}
					return arr;
				}
			 
				private static int[][] next2dArray(int n, int m) {
					int[][] arr = new int[n][m];
					for (int i = 0; i < n; i++) {
						for (int j = 0; j < m; j++) {
							arr[i][j] = nextInt();
						}
					}
					return arr;
				}
				private static char[][] nextCharArray(int n,int m){
					char [][]c=new char[n][m];
					for(int i=0;i<n;i++){
						String s=nextLine();
						for(int j=0;j<s.length();j++){
							c[i][j]=s.charAt(j);
						}
					}
					return c;
				}
			 
				private static long[] nextLongArray(int n) {
					long[] arr = new long[n];
					for (int i = 0; i < n; i++) {
						arr[i] = nextLong();
					}
					return arr;
				}
			 
				private static void pArray(int[] arr) {
					for (int i = 0; i < arr.length; i++) {
						pw.print(arr[i] + " ");
					}
					pw.println();
					return;
				}
			 
				private static void pArray(long[] arr) {
					for (int i = 0; i < arr.length; i++) {
						pw.print(arr[i] + " ");
					}
					pw.println();
					return;
				}
			 
				private static void pArray(boolean[] arr) {
					for (int i = 0; i < arr.length; i++) {
						pw.print(arr[i] + " ");
					}
					pw.println();
					return;
				}
			 
				private static boolean isSpaceChar(int c) {
					if (filter != null)
						return filter.isSpaceChar(c);
					return isWhitespace(c);
				}
			 
				private interface SpaceCharFilter {
					public boolean isSpaceChar(int ch);
				}
				private static void soln(){
				int n=nextInt();
			int ar[]=new int[n+1];
			for(int i=1;i<=n;i++){
				ar[i]=nextInt();
			}
			String[] s=new String[n+1];
			for(int i=1;i<=n;i++){
				s[i]=nextLine();
			}
			Dsu ds=new Dsu(n);
			
			for(int i=1;i<=n;i++){
				
				for(int j=0;j<s[i].length();j++){
					if(s[i].charAt(j)=='1'){
					//	pw.println(i+" "+(j+1)+"---");
							ds.union(i, j+1);
						
					}
				}
			}
		
			ArrayList<Integer> a[]=new ArrayList[n+1];
			for(int i=1;i<=n;i++){
				a[i]=new ArrayList<Integer>();
			}
	
			for(int i=1;i<=n;i++){
			int parent=ds.find(i);
			
				a[parent].add(i);
			
			}
			for(int i=1;i<=n;i++){
				Collections.sort(a[i]);
			}
			for(int i=1;i<=n;i++){
				
				if(a[i].size()!=0){
					TreeSet<Integer> ts=new TreeSet();
					
					for(int j=0;j<a[i].size();j++){
						ts.add(ar[a[i].get(j)]);	
					}
					
					for(int j=0;j<a[i].size();j++){
						ar[a[i].get(j)]=ts.first();
						ts.remove(ts.first());
					}

				}
			}
			for(int i=1;i<=n;i++){
				pw.print(ar[i]+" ");
			}
				}
			
				
				
			}
			class Dsu{
				private int rank[], parent[] ,n;
				
				Dsu(int size){
					this.n=size+1;
					rank=new int[n];
					parent=new int[n];
				makeSet();
				}
				
				void makeSet(){
					for(int i=0;i<n;i++){
						parent[i]=i;
					}
				}
				
				int find(int x){
					if(parent[x]!=x){
						
						parent[x]=find(parent[x]);
					}
					return parent[x];
				}	
				
				void union(int x,int y){
					int xRoot=find(x);
					int yRoot=find(y);
					
					if(xRoot==yRoot)
						return;
					if(rank[xRoot]<rank[yRoot]){
					parent[xRoot]=yRoot;	
					}else if(rank[yRoot]<rank[xRoot]){
						parent[yRoot]=xRoot;
					}else{
						parent[yRoot]=xRoot;
						rank[xRoot]++;
					}
					
				}
			 
			}
			class Pair implements Comparable<Pair>{
			
				int v,w;
				 Pair(int to,int dist) {
					 v=to;
					 w=dist;
					// TODO Auto-generated constructor stub
				
				}
				@Override
				public int compareTo(Pair arg0) {
					// TODO Auto-generated method stub
					return w-arg0.w;
				}
				
			}
			class Graph{
				private int V,ans=0;
				private boolean Visited[];
				private LinkedList<Node> adj[];
				private StringBuilder sb=new StringBuilder();
				Graph(int V){
					this.V=V++;
					Visited=new boolean[V];
					adj=new LinkedList[V];
					for(int i=0;i<V;i++){
						adj[i]=new LinkedList<Node>();
					}
				}
				public long getAns(int start,HashSet hs){
					long an=Long.MAX_VALUE;
					for(int i=0;i<adj[start].size();i++){
						if(!hs.contains(adj[start].get(i).v))
						an=Math.min(an, adj[start].get(i).dia);
					}
					return an;
				}
				public void print(){
					System.out.println(ans);
					System.out.println(sb);
				}
				void addEdge(int u,int v,int w){
					adj[u].add(new Node(v,w));
				}
				public void dfs(int startVertex){
					if(!Visited[startVertex]){
						dfsUtil(startVertex);
					}
				}
				public void dfsUtil(int startVertex){
					Visited[startVertex]=true;
					Stack<Integer> st=new Stack<Integer>();
					st.push(startVertex);
					int di=Integer.MAX_VALUE;
					int temp=0;
					while(!st.isEmpty()){
						int top=st.pop();
						temp=top;
						Iterator<Node> i=adj[top].listIterator();
						while(i.hasNext()){
							Node ne=i.next();
							int n=ne.v;
							di=Math.min(di, ne.dia);
							if(!Visited[n]){
								st.push(n);
								Visited[n]=true;
							}
						}
					}
				
					sb.append(startVertex+" "+temp+" "+di+"\n");
					ans++;
				}
				
			}
			
			class Node{
				int v,dia;
				Node(int to,int dist){
					v=to;
					dia=dist;
				}
				public int getV() {
					return v;
				}
				public int getDia() {
					return dia;
				}
				
			}

		
		
	
		
		
	 