import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public final class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int testCount = in.nextInt();
        for (int testCase = 1; testCase <= testCount; ++testCase) {
            int n = in.nextInt();

            List<Integer> array = new ArrayList(n);

            if (n % 4 == 0) {
                for (int x = 1, i = 0; i < n ; x += 5, i+= 4) {
                    array.add(x + 1);
                    array.add(x + 3);
                    array.add(x);
                    array.add(x + 4);
                }

                array.sort(Comparator.comparingInt(a -> a % 2));

                System.out.println("YES");
                for (int i = 0; i < n; i++) {
                    if (i > 0) {
                        System.out.print(" ");
                    }
                    System.out.print(array.get(i));
                }

                System.out.println();
            } else {
                System.out.println("NO");
            }

        }
    }

}
/*
5
2
4
6
8
10
* */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public final class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int testCount = in.nextInt();
        for (int testCase = 1; testCase <= testCount; ++testCase) {
            int n = in.nextInt();

            List<Integer> oddArray = new ArrayList<>(n);
            List<Integer> evenAarray = new ArrayList<>(n);

            if (n % 4 == 0) {
                for (int x = 1, i = 0; i < n ; x += 5, i+= 4) {
                    if (x % 2 != 0) {
                        oddArray.add(x);
                        evenAarray.add(x + 1);
                        evenAarray.add(x + 3);
                        oddArray.add(x + 4);
                    } else {
                        evenAarray.add(x);
                        oddArray.add(x + 1);
                        oddArray.add(x + 3);
                        evenAarray.add(x + 4);
                    }
                }

                System.out.println("YES");
                for (int i = 0; i < n/2; i++) {
                    if (i > 0) {
                        System.out.print(" ");
                    }
                    System.out.print(evenAarray.get(i));
                }

                for (int i = 0; i < n/2; i++) {
                    System.out.print(" " + oddArray.get(i));
                }

                System.out.println();
            } else {
                System.out.println("NO");
            }

        }
    }

}
/*
5
2
4
6
8
10
* */


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public final class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        int testCount = in.nextInt();
        for (int testCase = 1; testCase <= testCount; ++testCase) {
            int n = in.nextInt();

            List<Integer> oddArray = new ArrayList<>(n);
            List<Integer> evenAarray = new ArrayList<>(n);

            if (n % 4 == 0) {
                for (int x = 1, i = 0; i < n ; x += 5, i+= 4) {
                    if (x % 2 != 0) {
                        oddArray.add(x);
                        evenAarray.add(x + 1);
                        evenAarray.add(x + 3);
                        oddArray.add(x + 4);
                    } else {
                        evenAarray.add(x);
                        oddArray.add(x + 1);
                        oddArray.add(x + 3);
                        evenAarray.add(x + 4);
                    }
                }

                out.println("YES");
                for (int i = 0; i < n/2; i++) {
                    if (i > 0) {
                        out.print(" ");
                    }
                    out.print(evenAarray.get(i));
                }

                for (int i = 0; i < n/2; i++) {
                    out.print(" " + oddArray.get(i));
                }

                out.println();
            } else {
                out.println("NO");
            }
        }
        out.close();
    }

}
/*
5
2
4
6
8
10
* */


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class stone {
    public static void main(String args[]) {
       // System.out.println("enter the number of test cases");
        int t = 0;
        Scanner sc = new Scanner(System.in);
        t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            //System.out.println("enter the value of n");
            int n = sc.nextInt();
            if((n/2)%2!=0)
            {
                System.out.println("NO");
               continue;
            }
            int a[]=new int [n];
            int count =2;
            int sum=0;
            for(int j=0;j<n/2;j++)
            {
                a[j]=count;
                sum+=a[j];
                count+=2;
            }
            count =1;
            for(int z=n/2;z<n;z++)
            {
                if(z==n-1)
                {
                    a[z]=a[n/2-1]+((n/2)-1);
                    break;
                }
                a[z]=count;
                count+=2;
            }
            System.out.println("YES");
            for(int x=0;x<n;x++)
            {
                System.out.println(a[x]);
            }
        }
    }
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class stone {
    public static void main(String args[]) throws IOException {
       // System.out.println("enter the number of test cases");
        int t = 0;
        FastReader sc = new FastReader();
        BufferedWriter w=new BufferedWriter(new PrintWriter(System.out));
        t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            //System.out.println("enter the value of n");
            int n = sc.nextInt();
            if((n/2)%2!=0)
            {
                w.write("NO\n");
               continue;
            }
            int a[]=new int [n];
            int count =2;
            int sum=0;
            for(int j=0;j<n/2;j++)
            {
                a[j]=count;
                sum+=a[j];
                count+=2;
            }
            count =1;
            for(int z=n/2;z<n;z++)
            {
                if(z==n-1)
                {
                    a[z]=a[n/2-1]+((n/2)-1);
                    break;
                }
                a[z]=count;
                count+=2;
            }
            w.write("YES\n");
            for(int x=0;x<n;x++)
            {
                w.write(a[x]+" ");
            }
            w.write("\n");
        }
        w.close();
    }
}

class FastReader
{
    BufferedReader br;
    StringTokenizer st;

    public FastReader() throws IOException {
        br = new BufferedReader(new
                InputStreamReader(System.in));

    }

    String next() throws NumberFormatException,IOException,NullPointerException
    {
        while (st == null || !st.hasMoreElements())
        {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt()throws NumberFormatException,IOException,NullPointerException
    {
        return Integer.parseInt(next());
    }

    long nextLong()throws NumberFormatException,IOException,NullPointerException
    {
        return Long.parseLong(next());
    }

    double nextDouble()throws NumberFormatException,IOException,NullPointerException
    {
        return Double.parseDouble(next());
    }

    String nextLine()
    {
        String str = "";
        try
        {
            str = br.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str;
    }
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class stone {
    public static void main(String args[]) {
       // System.out.println("enter the number of test cases");
        int t = 0;
        Scanner sc = new Scanner(System.in);
        t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            //System.out.println("enter the value of n");
            int n = sc.nextInt();
            if((n/2)%2!=0)
            {
                System.out.println("NO");
               continue;
            }
            int a[]=new int [n];
            int count =2;
            int sum=0;
            for(int j=0;j<n/2;j++)
            {
                a[j]=count;
                sum+=a[j];
                count+=2;
            }
            count =1;
            for(int z=n/2;z<n;z++)
            {
                if(z==n-1)
                {
                    a[z]=a[n/2-1]+((n/2)-1);
                    break;
                }
                a[z]=count;
                count+=2;
            }
            for(int x=0;x<n;x++)
            {
                System.out.println("YES");
                System.out.println(a[x]);
            }
        }
    }
}


import java.util.Scanner;

public class Four_935A {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int t = sc.nextInt();
		int n;
		int res1=0;
		int res2=0;
		int a[] = null;
		int b[] = null;
		while(t>0){
			n = sc.nextInt();
			if((n/2)%2!=0){
				System.out.println("NO");
			}
			else {
				a = new int[n/2];
				b = new int[n/2];
				for(int i=0;i<n/2;i++){
					a[i]=2*(i+1);
					res1 = res1 + a[i];
				}
				for(int i=0;i<n/2;i++){
					if(i==n/2-1){
						b[i]=res1-res2;
					}
					else {
						b[i]= 2*(i+1)-1;
						res2 = res2+b[i];
					}
				}
				System.out.println("YES");
				for(int i=0;i<n/2;i++){
					System.out.print(a[i]+" ");
				}
				for(int i=0;i<n/2;i++){
					System.out.print(b[i]+" ");
				}
				System.out.println();
			}

			t--;
		}
		sc.close();
	}
}


import java.util.Scanner;

public class Four_935A {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int t = sc.nextInt();
		int n;
		int res1=0;
		int res2=0;
		int a[] = null;
		int b[] = null;
		while(t>0){
			n = sc.nextInt();
			if((n/2)%2!=0){
				System.out.println("NO");
			}
			else {
				res1=0;
				res2=0;
				a = new int[n/2];
				b = new int[n/2];
				for(int i=0;i<n/2;i++){
					a[i]=2*(i+1);
					res1 = res1 + a[i];
				}
				for(int i=0;i<n/2;i++){
					if(i==n/2-1){
						b[i]=res1-res2;
					}
					else {
						b[i]= 2*(i+1)-1;
						res2 = res2+b[i];
					}
				}
				System.out.println("YES");
				for(int i=0;i<n/2;i++){
					System.out.print(a[i]+" ");
				}
				for(int i=0;i<n/2;i++){
					System.out.print(b[i]+" ");
				}
				System.out.println();
			}

			t--;
		}
		sc.close();
	}
}


/*
Allen has a LOT of money. He has n dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are 1, 5, 10, 20, 100. What is the minimum number of bills Allen could receive after withdrawing his entire balance?

Input
The first and only line of input contains a single integer n (1≤n≤109).

Output
Output the minimum number of bills that Allen could receive.

Examples
inputCopy
125
outputCopy
3
inputCopy
43
outputCopy
5
inputCopy
1000000000
outputCopy
10000000
*/

package codeforces;

import java.util.Scanner;

public class Four_996A {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int count =0;

			if(n>=100){
				count = count + n/100;
				n= n%100;
			}
			if(n>=20){
				count = count + n/20;
				n= n%20;
			}
			if(n>=10){
				count = count + n/10;
				n= n%10;
			}
			if(n>=5){
				count = count + n/5;
				n= n%5;
			}
				count = count + n;

				System.out.println(count);
				sc.close();
	}
}


import java.util.Scanner;
import java.io.IOException;

public class CF_1343_A {
    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        int tN = s.nextInt();

        for(int t = 0; t < tN; t++) {
            int n = s.nextInt();
            if ((n % 4) != 0) {
                System.out.println("NO");
            }
            else {
                System.out.println("YES");

                for (int i = 1; i <= n / 2; i++) {
                    System.out.print(i * 2 + " ");
                }

                for (int i = 1; i <= (n / 2) - 1; i++) {
                    System.out.print((i * 2 - 1) + " ");
                }

                System.out.println( n / 2  + n - 1);
            }
        }
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class CF_1343_A {
    public static void main(String[] args) throws IOException {
        Reader s = new Reader();
        int tN = s.nextInt();

        for(int t = 0; t < tN; t++) {
            int n = s.nextInt();
            if ((n % 4) != 0) {
                System.out.println("NO");
            }
            else {
                System.out.println("YES");

                for (int i = 1; i <= n / 2; i++) {
                    System.out.print(i * 2 + " ");
                }

                for (int i = 1; i <= (n / 2) - 1; i++) {
                    System.out.print((i * 2 - 1) + " ");
                }

                System.out.println( n / 2  + n - 1);
            }
        }
    }

    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null)
                return;
            din.close();
        }
    }
}


import java.util.Scanner;

public class CF_1343_A {
    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        int tN = s.nextInt();

        for(int t = 0; t < tN; t++) {
            int n = s.nextInt();
            if ((n % 4) != 0) {
                System.out.println("NO");
            }
            else {
                System.out.println("YES");

                for (int i = 1; i <= n / 2; i++) {
                    System.out.print(i * 2 + " ");
                }

                for (int i = 1; i <= (n / 2) - 1; i++) {
                    System.out.print((i * 2 - 1) + " ");
                }

                System.out.println( n / 2  + n - 1);
            }
        }
    }
}


import java.util.*;
public class Solution3 {

	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int t= sc.nextInt();

		for(int j=0;j<t;j++) {
			int n = sc.nextInt();
			int a[]= new int[n];
			int s=0;
			if(n%4!=0)
			{
				System.out.println("No");
			}
			else
			{
				System.out.println("Yes");
				s=n*(n+2)/4;
				for(int i=1;i<=n/2;i++)
				{
				a[i-1]=2*i;
				}
				for(int i=n/2, k=0;i<n-1;i++,k++)
				{
					a[i]=2*k+1;
				}
				a[n-1]=3*n/2 -1;
				for(int i=0;i<n;i++)
					System.out.print(a[i]+" ");
				System.out.println();
			}
		}
	}

}


import java.util.*;
public class Solution {

	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int t= sc.nextInt();

		for(int j=0;j<t;j++) {
			int n = sc.nextInt();
			int a[]= new int[n];
			int s=0;
				s=n*(n+2)/4;
				int s1=0;
				int s2=0;
				for(int i=1;i<=n/2;i++)
				{
				a[i-1]=2*i;
				s1=s1+a[i-1];
				}
				for(int i=n/2, k=0;i<n-1;i++,k++)
				{
					a[i]=2*k+1;
					s2=s2+a[i];
				}
				a[n-1]=s-(n/2-1)*(n/2-1);
				s2=s2+a[n-1];
				if(s2==s1 && a[n-1]%2!=0) {
					System.out.println("Yes");

				for(int i=0;i<n;i++)
					System.out.print(a[i]+" ");
				System.out.println();
				}
				else {
					System.out.println("No");}}
		}
	}


import java.util.*;
public class Solution3 {

	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int t= sc.nextInt();

		for(int j=0;j<t;j++) {
			int n = sc.nextInt();
			int a[]= new int[n];
			int s=0;
			if(n%4!=0)
			{
				System.out.println("No");
			}

			else {
				s=n*(n+2)/4;
				int s1=0;
				int s2=0;
				for(int i=1;i<=n/2;i++)
				{
				a[i-1]=2*i;
				s1=s1+a[i-1];
				}
				for(int i=n/2, k=0;i<n-1;i++,k++)
				{
					a[i]=2*k+1;
					s2=s2+a[i];
				}
				a[n-1]=s-(n/2-1)*(n/2-1);
				s2=s2+a[n-1];
				if(s2==s1) {
					System.out.println("Yes");

				for(int i=0;i<n;i++)
					System.out.print(a[i]+" ");
				System.out.println();
				}
				else {
					System.out.println("No");}}
		}
	}

}


import java.util.*;
public class Solution {

	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int t= sc.nextInt();

		for(int j=0;j<t;j++) {
			int n = sc.nextInt();
			int a[]= new int[n];
			int s=0;
			if(n%4!=0)
			{
				System.out.println("No");
			}
			else
			{
				System.out.println("Yes");
				s=n*(n+2)/4;
				for(int i=1;i<=n/2;i++)
				{
				a[i-1]=2*i;
				}
				for(int i=n/2, k=0;i<n-1;i++,k++)
				{
					a[i]=2*k+1;
				}
				a[n-1]=s-(n/2-1)*(n/2-1);
				for(int i=0;i<n;i++)
					System.out.print(a[i]+" ");
				System.out.println();
			}
		}
	}

}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{

	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	StringBuilder sb=new StringBuilder();
	int t=Integer.parseInt(bf.readLine());
	while(t-->0)
	{
	    int n=Integer.parseInt(bf.readLine());
	    if(n%4==0)
	    {
	        sb.append("YES\n");
	        for(int i=1;i<=n/2;i++)
	        {
	            int k=2*i;
	            sb.append(k+" ");
	        }
	        for(int i=1;i<=(n/2)-1;i++)
	        {
	            int z=(2*i)-1;
	            sb.append(z+" ");
	        }
	        long san=( (n*(n+2))/4 )- ( ((n/2)-1)*((n/2)-1) );
	        sb.append(san+"\n");
	    }
	    else
	    {
	        sb.append("NO\n");
	    }
	}
	System.out.println(sb);
	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{

	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	StringBuilder sb=new StringBuilder();
	int t=Integer.parseInt(bf.readLine());
	while(t-->0)
	{
	    int n=Integer.parseInt(bf.readLine());
	   // long san=( (n*(n+2))/4 )- ( ((n/2)-1)*((n/2)-1) );
	    if(n%4==0)
	    {
	        sb.append("YES\n");
	        for(int i=1;i<=n/2;i++)
	        {
	            int k=2*i;
	            sb.append(k+" ");
	        }
	        for(int i=1;i<=(n/2)-1;i++)
	        {
	            int z=(2*i)-1;
	            sb.append(z+" ");
	        }
	        long san=( (n*1L*(n+2))/4 )- ( ((n/2)-1)*1L*((n/2)-1) );
	     //int p=(2*(n/2))-1+(n/2);
	        sb.append(san+"\n");
	    }
	    else
	    {
	        sb.append("NO\n");
	    }
	}
	System.out.println(sb);
	}
}


import java.awt.*;
import java.io.*;
import java.util.*;

public class Abc {
    public static void main(String[] args) throws Exception {
//        Scanner sc = new Scanner(System.in);
        FastReader sc=new FastReader();
        int n=sc.nextInt();
        par=new int[n];rank=new int[n];
        int p[]=new int[n];
        int ind[]=new int[n];
        for (int i=0;i<n;i++)makeSet(i);
        for (int i=0;i<n;i++){
            int x=sc.nextInt()-1;
            ind[x]=i;
            p[i]=x;
        }
        for (int i=0;i<n;i++){
            String s=sc.next();
            for (int j=0;j<n;j++){
                if (s.charAt(j)=='1'){
                    union(i,j);
                }
            }
        }

        int c=-1;
        for (int i=0;i<n;i++){
            if (ind[i]!=i) {
                if (find(ind[i]) == find(i)) {
                    int tmp=p[i];
                    p[ind[i]]=p[i];
                    p[i]=i;
                    int tmp1=ind[i];
                    ind[i]=ind[tmp];
                    ind[tmp]=tmp1;
                }else break;
            }
            c++;
        }
        int x=-1;
        if (c<n-1){
            x=p[c+1];
            for (int i=c+1;i<p[i];i++){
                if (find(c+1)==find(ind[i])){
                    x=i;
                    break;
                }
            }
            int tmp=p[c+1];
            p[c+1]=x;
            p[ind[x]]=tmp;

        }
        StringBuilder sb=new StringBuilder();
        for (int i=0;i<n;i++){
            sb.append((p[i]+1)+" ");
        }
        System.out.println(sb);

    }
    static int rank[];
    static int par[];
    static void makeSet(int v){
        par[v]=v;
        rank[v]=0;
    }
    static int find(int v){
        if (par[v]==v)return v;
        return par[v]=find(par[v]);
    }
    static void union(int v,int u){
        v=find(v);
        u=find(u);
        if (v!=u){
            if (rank[v]<rank[u]){
                int tmp=u;
                u=v;
                v=tmp;
            }
            par[u]=v;
            if (rank[u]==rank[v])rank[v]++;
        }
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class codeforces
{
   static class Student{
        double e;
       String s;
        Student(String s,double e){
            this.s=s;
            this.e=e;
        }
    }
    static int sieveOfEratosthenes(int n)
    {
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        int pos=0;
        boolean prime[] = new boolean[n+1];
        for(int i=0;i<=n;i++)
            prime[i] = true;

        for(int p = 2; p*p <=n; p++)
        {
            // If prime[p] is not changed, then it is a prime
            if(prime[p] == true)
            {
                // Update all multiples of p
                for(int i = p*p; i <= n; i += p)
                    prime[i] = false;
            }
        }

        // Print all prime numbers
        for(int i = 2; i <= n; i++)
        {
            if(prime[i] == true)
                ++pos;
        }
        return pos;
    }
static    class Sortbyroll implements Comparator<Student>
{
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student c, Student b)
    {
        String s1,s2;
        s1=c.s.toLowerCase();
        s2=b.s.toLowerCase();
    if(c.e>b.e)
    return -1;
    if(c.e<b.e)
    return 1;
    return s1.compareTo(s2);
    }
}
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    static class Edge{
        int a,b;
        Edge(int a,int b){
            this.a=a;
            this.b=b;
        }
    }
    public static void main(String[] args){
        //long sum=0;
        int t,i,j,k,l,pos,n;
        String r,p;
        FastReader sc=new FastReader();
        n=sc.nextInt();
        int a[]=new int[n];
        for(i=0;i<n;i++)
            a[i]=sc.nextInt()-1;
        String s[]=new String[n];
        for(i=0;i<n;i++){
            s[i]=sc.nextLine();
        }
        int b[]=new int[n];
        Queue<Integer>q=new LinkedList<>();
        ArrayList<Integer>v;
        ArrayList<Integer>val;
        int ans[]=new int[n];
        for(i=0;i<n;i++){
            if(b[i]==0){
                b[i]=1;
                v=new ArrayList<>();
                val=new ArrayList<>();
                v.add(i);
                val.add(a[i]);
                q.add(i);
                while(!q.isEmpty()){
                    for(j=0;j<n;j++){
                        if(b[j]==0&&s[q.peek()].charAt(j)=='1'){
                            b[j]=1;
                            v.add(j);
                            val.add(a[j]);
                            q.add(j);
                        }
                    }
                    q.poll();
                }
                Collections.sort(v);
                Collections.sort(val);
                      /*  for(i=0;i<v.size();i++)
            System.out.print(val.get(i)+" ");
                break;*/
                for(j=0;j<v.size();j++){
                    ans[v.get(j)]=val.get(j)+1;
                }
            }
        }
        for(i=0;i<n;i++)
            System.out.print(ans[i]+" ");
    }
 static long power(long x, long y, long p)
{
    long res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if ((y & (long)1)%2!=0)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res%p;
}
   static int find(int x,int parent[])
    {
        // Finds the representative of the set
        // that x is an element of
        while(parent[x]!=x)
        {
            // if x is not the parent of itself
            // Then x is not the representative of
            // his set,
            x=parent[x];

            // so we recursively call Find on its parent
            // and move i's node directly under the
            // representative of this set
        }

        return x;
    }
    static void union(int x, int y,int rank[],int parent[])
    {
        // Find representatives of two sets
        int xRoot = find(x,parent), yRoot = find(y,parent);

        // Elements are in the same set, no need
        // to unite anything.
        if (xRoot == yRoot)
            return;

         // If x's rank is less than y's rank
        if (rank[xRoot] < rank[yRoot])

            // Then move x under y  so that depth
            // of tree remains less
            parent[xRoot] = yRoot;

        // Else if y's rank is less than x's rank
        else if (rank[yRoot] < rank[xRoot])

            // Then move y under x so that depth of
            // tree remains less
            parent[yRoot] = xRoot;

        else // if ranks are the same
        {
            // Then move y under x (doesn't matter
            // which one goes where)
            parent[yRoot] = xRoot;

            // And increment the the result tree's
            // rank by 1
            rank[xRoot] = rank[xRoot] + 1;
        }
    }
    static long gcd(long a, long b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.regex.*;
public class Myclass {
         public static ArrayList adj[]=new ArrayList[300001];
         static int parent[]=new int[1000001];
         static int n;
		static void ini() {
			for(int i=0;i<n;i++) {
				parent[i]=i;
			}
		}
		static int root (int a) {
			while(parent[a]!=a) {
				a=parent[parent[a]];
			}
			return a;
		}
		static void uni(int a,int b) {
			int root_a=root(a);
			int root_b=root(b);
			if(root_a<root_b) {
				parent[root_b]=root_a;
			}
			else
				parent[root_a]=root_b;
		}
		static boolean find(int a,int b) {
			if(root(a)==root(b)) {
				return true;
			}
			else
				return false;
		}
		public static void main(String[] args)
        {
            InputReader in = new InputReader(System.in);
            PrintWriter pw = new PrintWriter(System.out);
            n=in.nextInt();
            ini();
            for(int i=0;i<n;i++) {
            	adj[i]=new ArrayList<Integer>();
            }
            int a[]=new int [n];
            for(int i=0;i<n;i++) {
            	a[i]=in.nextInt();
            }
            char s[][]=new char[n][n];
            for(int i=0;i<n;i++)
            	s[i]=in.nextLine().toCharArray();
            for(int i=0;i<n;i++) {
            	for(int j=0;j<n;j++) {
            		if(s[i][j]=='1')
            			uni(i,j);
            	}
            }
            Vector<Integer>v=new Vector<>();
            for(int i=0;i<n;i++) {
            	if(parent[i]==i) {
            		adj[i].add(i);
            		v.add(i);
            	}
            	else
            	{
            		for(int j=0;j<v.size();j++) {
            			if(find(v.get(j),i)) {
            				adj[j].add(i);
            				break;
            			}
            		}
            	}
            }
            int b[]=new  int[n];
            for(int i=0;i<v.size();i++) {
            	PriorityQueue <Integer>pq=new PriorityQueue<>();
            	for(int j=0;j<adj[v.get(i)].size();j++) {
            		pq.add(a[(int) adj[v.get(i)].get(j)]);
            	}
            	for(int j=0;j<adj[v.get(i)].size();j++) {
            		b[(int) adj[v.get(i)].get(j)]=pq.poll();
            	}
            }
            for(int i=0;i<n;i++) {
            	pw.print(b[i]+" ");
            }
            pw.flush();
            pw.close();

        }

        static class InputReader
        {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }
        public int snext()
        {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars)
            {
                curChar = 0;
                try
                {
                    snumChars = stream.read(buf);
                }

                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt()
        {
            int c = snext();
            while (isSpaceChar(c))
            {
                c = snext();
            }
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = snext();
            }
            int res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = snext();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong()
        {
            int c = snext();
            while (isSpaceChar(c))
            {
                c = snext();
            }
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = snext();
            }
            long res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = snext();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n)
        {
            int a[] = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = nextInt();
            }
            return a;
        }
        static class tri implements Comparable<tri> {
            int p, c, l;

            tri(int p, int c, int l) {
                this.p = p;
                this.c = c;
                this.l = l;
            }

            public int compareTo(tri o) {
                return Integer.compare(l, o.l);
            }
        }

        public String readString()
        {
            int c = snext();
            while (isSpaceChar(c))
            {
                c = snext();
            }
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = snext();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine()
        {
            int c = snext();
            while (isSpaceChar(c))
                c = snext();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = snext();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c)
        {
            return c == '\n' || c == '\r' || c == -1;
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }
        public static long mod = 1000000007;
        public static int d;
        public static int p;
        public static int q;

        public static int[] suffle(int[] a,Random gen)
        {
            int n = a.length;
            for(int i=0;i<n;i++)
            {
                int ind = gen.nextInt(n-i)+i;
                int temp = a[ind];
                a[ind] = a[i];
                a[i] = temp;
            }
            return a;
        }

        public static void swap(int a, int b){
            int temp = a;
            a = b;
            b = temp;
        }

        public static HashSet<Integer> primeFactorization(int n)
        {
            HashSet<Integer> a =new HashSet<Integer>();
            for(int i=2;i*i<=n;i++)
            {
                while(n%i==0)
                {
                    a.add(i);
                    n/=i;
                }
            }
            if(n!=1)
                a.add(n);
            return a;
        }

        public static void sieve(boolean[] isPrime,int n)
        {
            for(int i=1;i<n;i++)
                isPrime[i] = true;

            isPrime[0] = false;
            isPrime[1] = false;

            for(int i=2;i*i<n;i++)
            {
                if(isPrime[i] == true)
                {
                    for(int j=(2*i);j<n;j+=i)
                        isPrime[j] = false;
                }
            }
        }

        public static int GCD(int a,int b)
        {
            if(b==0)
                return a;
            else
                return GCD(b,a%b);
        }

        public static long GCD(long a,long b)
        {
            if(b==0)
                return a;
            else
                return GCD(b,a%b);
        }

        public static void extendedEuclid(int A,int B)
        {
            if(B==0)
            {
                d = A;
                p = 1 ;
                q = 0;
            }
            else
            {
                extendedEuclid(B, A%B);
                int temp = p;
                p = q;
                q = temp - (A/B)*q;
            }
        }

        public static long LCM(long a,long b)
        {
            return (a*b)/GCD(a,b);
        }

        public static int LCM(int a,int b)
        {
            return (a*b)/GCD(a,b);
        }

        public static int binaryExponentiation(int x,int n)
        {
            int result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=result * x;
                x=x*x;
                n=n/2;
            }
            return result;
        }

        public static long binaryExponentiation(long x,long n)
        {
            long result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=result * x;
                x=x*x;
                n=n/2;
            }
            return result;
        }

        public static int modularExponentiation(int x,int n,int M)
        {
            int result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=(result * x)%M;
                x=(x%M*x)%M;
                n=n/2;
            }
            return result;
        }

        public static long modularExponentiation(long x,long n,long M)
        {
            long result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=(result%M * x%M)%M;
                x=(x%M * x%M)%M;
                n=n/2;
            }
            return result;
        }

        public static long modInverse(int A,int M)
        {
            return modularExponentiation(A,M-2,M);
        }

        public static long modInverse(long A,long M)
        {
            return modularExponentiation(A,M-2,M);
        }

        public static boolean isPrime(int n)
        {

            if (n <= 1)  return false;
            if (n <= 3)  return true;

            if (n%2 == 0 || n%3 == 0)
                return false;

            for (int i=5; i*i<=n; i=i+6)
            {
                if (n%i == 0 || n%(i+2) == 0)
                   return false;
            }

            return true;
        }

         public static long[] shuffle(long[] a, Random gen){
                for(int i = 0, n = a.length;i < n;i++){
                    int ind = gen.nextInt(n-i)+i;
                    long d = a[i];
                    a[i] = a[ind];
                    a[ind] = d;
                }
                return a;
            }
        static class pair implements Comparable<pair>
        {
            Long x;
            Long y;
            pair(long a,long sum)
            {
                this.x=a;
                this.y=sum;
            }

        public int compareTo(pair o) {
                int result = x.compareTo(o.x);
                if(result==0)
                    result = y.compareTo(o.y);

            return result;
            }

            public String toString()
            {
                return x+" "+y;
            }

            public boolean equals(Object o)
            {
                if (o instanceof pair)
                {
                    pair p = (pair)o;
                    return p.x == x && p.y == y ;
                }
                return false;
            }

            public int hashCode()
            {
                return new Long(x).hashCode()*31 + new Long(y).hashCode();
            }
        }

}


import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.regex.*;
public class Myclass {
         public static ArrayList adj[]=new ArrayList[300001];
         static int parent[]=new int[1000001];
         static int n;
		static void ini() {
			for(int i=0;i<n;i++) {
				parent[i]=i;
			}
		}
		static int root (int a) {
			while(parent[a]!=a) {
				a=parent[parent[a]];
			}
			return a;
		}
		static void uni(int a,int b) {
			int root_a=root(a);
			int root_b=root(b);
			if(root_a<root_b) {
				parent[root_b]=root_a;
			}
			else
				parent[root_a]=root_b;
		}
		static boolean find(int a,int b) {
			if(root(a)==root(b)) {
				return true;
			}
			else
				return false;
		}
		public static void main(String[] args)
        {
            InputReader in = new InputReader(System.in);
            PrintWriter pw = new PrintWriter(System.out);
            n=in.nextInt();
            ini();
            for(int i=0;i<n;i++) {
            	adj[i]=new ArrayList<Integer>();
            }
            int a[]=new int [n];
            for(int i=0;i<n;i++) {
            	a[i]=in.nextInt();
            }
            char s[][]=new char[n][n];
            for(int i=0;i<n;i++)
            	s[i]=in.nextLine().toCharArray();
            for(int i=0;i<n;i++) {
            	for(int j=0;j<n;j++) {
            		if(s[i][j]=='1')
            			uni(i,j);
            	}
            }
            Vector<Integer>v=new Vector<>();
            for(int i=0;i<n;i++) {
            	if(parent[i]==i) {
            		adj[i].add(i);
            		v.add(i);
            	}
            	else
            	{
            		for(int j=0;j<v.size();j++) {
            			if(find(v.get(j),i)) {
            				adj[v.get(j)].add(i);
            				break;
            			}
            		}
            	}
            }
           // for(int i=0;i<n;i++)
          //  	pw.println(adj[i].toString());
            int b[]=new  int[n];
            for(int i=0;i<v.size();i++) {
            	PriorityQueue <Integer>pq=new PriorityQueue<>();
            	for(int j=0;j<adj[v.get(i)].size();j++) {
            		pq.add(a[(int) adj[v.get(i)].get(j)]);
            	}
            	for(int j=0;j<adj[v.get(i)].size();j++) {
            		b[(int) adj[v.get(i)].get(j)]=pq.poll();
            	}
            }
            for(int i=0;i<n;i++) {
            	pw.print(b[i]+" ");
            }
            pw.flush();
            pw.close();

        }

        static class InputReader
        {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }
        public int snext()
        {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars)
            {
                curChar = 0;
                try
                {
                    snumChars = stream.read(buf);
                }

                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt()
        {
            int c = snext();
            while (isSpaceChar(c))
            {
                c = snext();
            }
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = snext();
            }
            int res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = snext();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong()
        {
            int c = snext();
            while (isSpaceChar(c))
            {
                c = snext();
            }
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = snext();
            }
            long res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = snext();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n)
        {
            int a[] = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = nextInt();
            }
            return a;
        }
        static class tri implements Comparable<tri> {
            int p, c, l;

            tri(int p, int c, int l) {
                this.p = p;
                this.c = c;
                this.l = l;
            }

            public int compareTo(tri o) {
                return Integer.compare(l, o.l);
            }
        }

        public String readString()
        {
            int c = snext();
            while (isSpaceChar(c))
            {
                c = snext();
            }
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = snext();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine()
        {
            int c = snext();
            while (isSpaceChar(c))
                c = snext();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = snext();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c)
        {
            return c == '\n' || c == '\r' || c == -1;
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }
        public static long mod = 1000000007;
        public static int d;
        public static int p;
        public static int q;

        public static int[] suffle(int[] a,Random gen)
        {
            int n = a.length;
            for(int i=0;i<n;i++)
            {
                int ind = gen.nextInt(n-i)+i;
                int temp = a[ind];
                a[ind] = a[i];
                a[i] = temp;
            }
            return a;
        }

        public static void swap(int a, int b){
            int temp = a;
            a = b;
            b = temp;
        }

        public static HashSet<Integer> primeFactorization(int n)
        {
            HashSet<Integer> a =new HashSet<Integer>();
            for(int i=2;i*i<=n;i++)
            {
                while(n%i==0)
                {
                    a.add(i);
                    n/=i;
                }
            }
            if(n!=1)
                a.add(n);
            return a;
        }

        public static void sieve(boolean[] isPrime,int n)
        {
            for(int i=1;i<n;i++)
                isPrime[i] = true;

            isPrime[0] = false;
            isPrime[1] = false;

            for(int i=2;i*i<n;i++)
            {
                if(isPrime[i] == true)
                {
                    for(int j=(2*i);j<n;j+=i)
                        isPrime[j] = false;
                }
            }
        }

        public static int GCD(int a,int b)
        {
            if(b==0)
                return a;
            else
                return GCD(b,a%b);
        }

        public static long GCD(long a,long b)
        {
            if(b==0)
                return a;
            else
                return GCD(b,a%b);
        }

        public static void extendedEuclid(int A,int B)
        {
            if(B==0)
            {
                d = A;
                p = 1 ;
                q = 0;
            }
            else
            {
                extendedEuclid(B, A%B);
                int temp = p;
                p = q;
                q = temp - (A/B)*q;
            }
        }

        public static long LCM(long a,long b)
        {
            return (a*b)/GCD(a,b);
        }

        public static int LCM(int a,int b)
        {
            return (a*b)/GCD(a,b);
        }

        public static int binaryExponentiation(int x,int n)
        {
            int result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=result * x;
                x=x*x;
                n=n/2;
            }
            return result;
        }

        public static long binaryExponentiation(long x,long n)
        {
            long result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=result * x;
                x=x*x;
                n=n/2;
            }
            return result;
        }

        public static int modularExponentiation(int x,int n,int M)
        {
            int result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=(result * x)%M;
                x=(x%M*x)%M;
                n=n/2;
            }
            return result;
        }

        public static long modularExponentiation(long x,long n,long M)
        {
            long result=1;
            while(n>0)
            {
                if(n % 2 ==1)
                    result=(result%M * x%M)%M;
                x=(x%M * x%M)%M;
                n=n/2;
            }
            return result;
        }

        public static long modInverse(int A,int M)
        {
            return modularExponentiation(A,M-2,M);
        }

        public static long modInverse(long A,long M)
        {
            return modularExponentiation(A,M-2,M);
        }

        public static boolean isPrime(int n)
        {

            if (n <= 1)  return false;
            if (n <= 3)  return true;

            if (n%2 == 0 || n%3 == 0)
                return false;

            for (int i=5; i*i<=n; i=i+6)
            {
                if (n%i == 0 || n%(i+2) == 0)
                   return false;
            }

            return true;
        }

         public static long[] shuffle(long[] a, Random gen){
                for(int i = 0, n = a.length;i < n;i++){
                    int ind = gen.nextInt(n-i)+i;
                    long d = a[i];
                    a[i] = a[ind];
                    a[ind] = d;
                }
                return a;
            }
        static class pair implements Comparable<pair>
        {
            Long x;
            Long y;
            pair(long a,long sum)
            {
                this.x=a;
                this.y=sum;
            }

        public int compareTo(pair o) {
                int result = x.compareTo(o.x);
                if(result==0)
                    result = y.compareTo(o.y);

            return result;
            }

            public String toString()
            {
                return x+" "+y;
            }

            public boolean equals(Object o)
            {
                if (o instanceof pair)
                {
                    pair p = (pair)o;
                    return p.x == x && p.y == y ;
                }
                return false;
            }

            public int hashCode()
            {
                return new Long(x).hashCode()*31 + new Long(y).hashCode();
            }
        }

}


import javafx.util.Pair;

import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Map.Entry;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class abc {

		static int[][] graph;
		static int[] ans;

		public static void main(String[] args) throws Exception {
			// TODO Auto-generated method stub
			InputReader s = new InputReader(System.in);
			PrintWriter p = new PrintWriter(System.out);
			int n=s.nextInt();
			int[] arr=new int[n];
			for(int i=0 ; i<arr.length ; i++) {
				arr[i]=s.nextInt()-1;
			}
			graph=new int[n][n];
			ans=new int[n];
			for(int i=0 ; i<n ; i++) {
				String str=s.nextLine();
				for(int j=0 ; j<n ; j++) {
					graph[i][j]=str.charAt(j)-'0';
				}
			}
			Arrays.fill(ans, -1);
			boolean[] vis=new boolean[n];
			for(int i=0 ; i<n ; i++) {
				Arrays.fill(vis, false);
				min=i;
				int e=dfs(vis,i,arr);
				ans[i]=arr[min];
				int temp=arr[i];
				arr[i]=arr[min];
				arr[min]=temp;
			}
			for(int i:ans) {
				System.out.print(i+1+" ");
			}
			System.out.println();
		}
		static int min;
		public static int dfs(boolean[] vis , int s , int[] arr) {
			vis[s]=true;
			if(arr[s]<arr[min] && ans[s]==-1) {
				min=s;
			}

		//	System.out.println(1);
			for(int j=0 ; j<graph.length ; j++) {
				if(vis[j]) {
					continue;
				}
				if(j==s || graph[s][j]==0) {
					continue;
				}
				int q=dfs(vis,j,arr);
			}

			return min;
		}

		private static boolean possible(long c, long m, long x, long sum) {
			// TODO Auto-generated method stub
			if(c<sum || m<sum) {
				return false;
			}
			if(c+m+x<sum) {
				return false;
			}
			return true;
		}
		public class Pair implements Comparable<Pair>{
	        int x;
	        int f;
	        public Pair(int a , int b){
	            x=a;
	            f=b;
	        }

	        public int comprae(Pair p){
	            return p.f-this.f;
	        }

			@Override
			public int compareTo(Pair arg0) {
				// TODO Auto-generated method stub
				return 0;
			}
	    }

		static long power(long x, long y)
	    {
			long temp=0l;
	        if( y == 0)
	            return 1l;
	        temp = power(x, y/2);

	        if (y%2 == 0)
	            return temp*temp;
	        else
	        {
	            if(y > 0)
	                return x * temp * temp;
	            else
	                return (temp * temp) / x;
	        }
	    }

		public static boolean helper(int[] arr1 , int[] arr2) {
			boolean[] temp=new boolean[3];
			if(arr1.length==0) return true;
			if(arr1[0]!=arr2[0]) return false;
			temp[arr1[0]+1]=true;
			for(int i=1 ; i<arr1.length ; i++) {

				if(arr1[i]==arr2[i]) {

				}else if(arr1[i]<arr2[i]) {
					if(!temp[2]) {
						return false;
					}
				}else {
					if(!temp[0]) {
						return false;
					}
				}
				temp[arr1[i]+1]=true;
			}
			return true;
		}

		public static ArrayList Divisors(long n) {
			ArrayList<Long> div = new ArrayList<>();

			for (long i = 1; i <= Math.sqrt(n); i++) {
				if (n % i == 0) {
					div.add(i);

					if (n / i != i)
						div.add(n / i);
				}
			}
			return div;
		}

		public static int BinarySearch(long[] a, long k) {
			int n = a.length;
			int i = 0, j = n - 1;
			int mid = 0;

			if (k < a[0])
				return 0;
			else if (k >= a[n - 1])
				return n;
			else {
				while (j - i > 1) {
					mid = (i + j) / 2;

					if (k >= a[mid])
						i = mid;
					else
						j = mid;
				}
			}

			return i + 1;
		}

		public static long GCD(long a, long b) {
			if (b == 0)
				return a;
			else
				return GCD(b, a % b);
		}

		public static long LCM(long a, long b) {
			return (a * b) / GCD(a, b);
		}

		static class InputReader {

			private final InputStream stream;
			private final byte[] buf = new byte[8192];
			private int curChar, snumChars;
			private SpaceCharFilter filter;

			public InputReader(InputStream stream) {
				this.stream = stream;
			}

			public int snext() {
				if (snumChars == -1)
					throw new InputMismatchException();
				if (curChar >= snumChars) {
					curChar = 0;
					try {
						snumChars = stream.read(buf);
					} catch (IOException e) {
						throw new InputMismatchException();
					}
					if (snumChars <= 0)
						return -1;
				}
				return buf[curChar++];
			}

			static class pair implements Comparable<pair> {
				Integer x, y;

				pair(int x, int y) {
					this.x = x;
					this.y = y;
				}

				public int compareTo(pair o) {
					int result = x.compareTo(o.x);
					if (result == 0)
						result = y.compareTo(o.y);

					return result;
				}

				public String toString() {
					return x + " " + y;
				}

				public boolean equals(Object o) {
					if (o instanceof pair) {
						pair p = (pair) o;
						return p.x - x == 0 && p.y - y == 0;
					}
					return false;
				}

				public int hashCode() {
					return new Long(x).hashCode() * 31 + new Long(y).hashCode();
				}
			}

			public int nextInt() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				int res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public long nextLong() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				long res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public int[] nextIntArray(int n) {
				int a[] = new int[n];
				for (int i = 0; i < n; i++) {
					a[i] = nextInt();
				}
				return a;
			}

			public String readString() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isSpaceChar(c));
				return res.toString();
			}

			public String nextLine() {
				int c = snext();
				while (isSpaceChar(c))
					c = snext();
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isEndOfLine(c));
				return res.toString();
			}

			public boolean isSpaceChar(int c) {
				if (filter != null)
					return filter.isSpaceChar(c);
				return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
			}

			private boolean isEndOfLine(int c) {
				return c == '\n' || c == '\r' || c == -1;
			}

			public interface SpaceCharFilter {
				public boolean isSpaceChar(int ch);
			}
		}

		static class CodeX {
			public static void sort(long arr[]) {
				merge_sort(arr, 0, arr.length - 1);
			}

			private static void merge_sort(long A[], long start, long end) {
				if (start < end) {
					long mid = (start + end) / 2;
					merge_sort(A, start, mid);
					merge_sort(A, mid + 1, end);
					merge(A, start, mid, end);
				}

			}

			private static void merge(long A[], long start, long mid, long end) {
				long p = start, q = mid + 1;
				long Arr[] = new long[(int) (end - start + 1)];
				long k = 0;

				for (int i = (int) start; i <= end; i++) {
					if (p > mid)
						Arr[(int) k++] = A[(int) q++];

					else if (q > end)
						Arr[(int) k++] = A[(int) p++];

					else if (A[(int) p] < A[(int) q])
						Arr[(int) k++] = A[(int) p++];

					else
						Arr[(int) k++] = A[(int) q++];
				}
				for (int i = 0; i < k; i++) {
					A[(int) start++] = Arr[i];
				}

			}

		}

	}


import javafx.util.Pair;

import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Map.Entry;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class abc {

		static int[][] graph;
		static int[] ans;

		public static void main(String[] args) throws Exception {
			// TODO Auto-generated method stub
			InputReader s = new InputReader(System.in);
			PrintWriter p = new PrintWriter(System.out);
			int n=s.nextInt();
			int[] arr=new int[n];
			for(int i=0 ; i<arr.length ; i++) {
				arr[i]=s.nextInt()-1;
			}
			graph=new int[n][n];
			ans=new int[n];
			for(int i=0 ; i<n ; i++) {
				String str=s.nextLine();
				for(int j=0 ; j<n ; j++) {
					graph[i][j]=str.charAt(j)-'0';
				}
			}
			Arrays.fill(ans, -1);
			boolean[] vis=new boolean[n];
			for(int i=0 ; i<n ; i++) {
				Arrays.fill(vis, false);
				min=i;
				int e=dfs(vis,i,arr);
				ans[i]=arr[min];
				int temp=arr[i];
				arr[i]=arr[min];
				arr[min]=temp;
			}
			for(int i:ans) {
				System.out.print(i+1+" ");
			}
			System.out.println();
		}
		static int min;
		public static int dfs(boolean[] vis , int s , int[] arr) {
			vis[s]=true;
			if(arr[s]<arr[min] && ans[s]==-1) {
				min=s;
			}

		//	System.out.println(1);
			for(int j=0 ; j<graph.length ; j++) {
				if(vis[j]) {
					continue;
				}
				if(j==s || graph[s][j]==0) {
					continue;
				}
				int q=dfs(vis,j,arr);
			}

			return min;
		}

		private static boolean possible(long c, long m, long x, long sum) {
			// TODO Auto-generated method stub
			if(c<sum || m<sum) {
				return false;
			}
			if(c+m+x<sum) {
				return false;
			}
			return true;
		}
		public class Pair implements Comparable<Pair>{
	        int x;
	        int f;
	        public Pair(int a , int b){
	            x=a;
	            f=b;
	        }

	        public int comprae(Pair p){
	            return p.f-this.f;
	        }

			@Override
			public int compareTo(Pair arg0) {
				// TODO Auto-generated method stub
				return 0;
			}
	    }

		public static int helper(String[] arr , int pos , int k , int[][] dp) {
			if(pos==arr.length && k==0) {
				return 1;
			}
			if(pos==arr.length || k==0) return 0;
			if(dp[pos][k]!=-1) return dp[pos][k];
			for(int i=9 ; i>=0 ; i--) {
				String s1=temp[i];
				String curr=arr[pos];
				int con=convert(curr,s1);
				if(con==-1 || con>k) {
					continue;
				}
				ans[pos]=(char)(i+'0');
				int z=helper(arr,pos+1,k-con,dp);
				if(z==1) {
					dp[pos][k]=1;
					return 1;
				}
				ans[pos]='z';
			}
			return dp[pos][k]=0;
		}

		public static int convert(String curr , String s1) {
			int count=0;
			for(int i=0 ; i<7 ; i++) {
				char c1=curr.charAt(i);
				char c2=s1.charAt(i);
				if(c1=='1' && c2=='0') {
					return -1;
				}
				if(c1=='0' && c2=='1') {
					count++;
				}
			}
			return count;
		}

		static long power(long x, long y)
	    {
			long temp=0l;
	        if( y == 0)
	            return 1l;
	        temp = power(x, y/2);

	        if (y%2 == 0)
	            return temp*temp;
	        else
	        {
	            if(y > 0)
	                return x * temp * temp;
	            else
	                return (temp * temp) / x;
	        }
	    }

		public static boolean helper(int[] arr1 , int[] arr2) {
			boolean[] temp=new boolean[3];
			if(arr1.length==0) return true;
			if(arr1[0]!=arr2[0]) return false;
			temp[arr1[0]+1]=true;
			for(int i=1 ; i<arr1.length ; i++) {

				if(arr1[i]==arr2[i]) {

				}else if(arr1[i]<arr2[i]) {
					if(!temp[2]) {
						return false;
					}
				}else {
					if(!temp[0]) {
						return false;
					}
				}
				temp[arr1[i]+1]=true;
			}
			return true;
		}

		public static ArrayList Divisors(long n) {
			ArrayList<Long> div = new ArrayList<>();

			for (long i = 1; i <= Math.sqrt(n); i++) {
				if (n % i == 0) {
					div.add(i);

					if (n / i != i)
						div.add(n / i);
				}
			}
			return div;
		}

		public static int BinarySearch(long[] a, long k) {
			int n = a.length;
			int i = 0, j = n - 1;
			int mid = 0;

			if (k < a[0])
				return 0;
			else if (k >= a[n - 1])
				return n;
			else {
				while (j - i > 1) {
					mid = (i + j) / 2;

					if (k >= a[mid])
						i = mid;
					else
						j = mid;
				}
			}

			return i + 1;
		}

		public static long GCD(long a, long b) {
			if (b == 0)
				return a;
			else
				return GCD(b, a % b);
		}

		public static long LCM(long a, long b) {
			return (a * b) / GCD(a, b);
		}

		static class InputReader {

			private final InputStream stream;
			private final byte[] buf = new byte[8192];
			private int curChar, snumChars;
			private SpaceCharFilter filter;

			public InputReader(InputStream stream) {
				this.stream = stream;
			}

			public int snext() {
				if (snumChars == -1)
					throw new InputMismatchException();
				if (curChar >= snumChars) {
					curChar = 0;
					try {
						snumChars = stream.read(buf);
					} catch (IOException e) {
						throw new InputMismatchException();
					}
					if (snumChars <= 0)
						return -1;
				}
				return buf[curChar++];
			}

			static class pair implements Comparable<pair> {
				Integer x, y;

				pair(int x, int y) {
					this.x = x;
					this.y = y;
				}

				public int compareTo(pair o) {
					int result = x.compareTo(o.x);
					if (result == 0)
						result = y.compareTo(o.y);

					return result;
				}

				public String toString() {
					return x + " " + y;
				}

				public boolean equals(Object o) {
					if (o instanceof pair) {
						pair p = (pair) o;
						return p.x - x == 0 && p.y - y == 0;
					}
					return false;
				}

				public int hashCode() {
					return new Long(x).hashCode() * 31 + new Long(y).hashCode();
				}
			}

			public int nextInt() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				int res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public long nextLong() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				long res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public int[] nextIntArray(int n) {
				int a[] = new int[n];
				for (int i = 0; i < n; i++) {
					a[i] = nextInt();
				}
				return a;
			}

			public String readString() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isSpaceChar(c));
				return res.toString();
			}

			public String nextLine() {
				int c = snext();
				while (isSpaceChar(c))
					c = snext();
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isEndOfLine(c));
				return res.toString();
			}

			public boolean isSpaceChar(int c) {
				if (filter != null)
					return filter.isSpaceChar(c);
				return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
			}

			private boolean isEndOfLine(int c) {
				return c == '\n' || c == '\r' || c == -1;
			}

			public interface SpaceCharFilter {
				public boolean isSpaceChar(int ch);
			}
		}

		static class CodeX {
			public static void sort(long arr[]) {
				merge_sort(arr, 0, arr.length - 1);
			}

			private static void merge_sort(long A[], long start, long end) {
				if (start < end) {
					long mid = (start + end) / 2;
					merge_sort(A, start, mid);
					merge_sort(A, mid + 1, end);
					merge(A, start, mid, end);
				}

			}

			private static void merge(long A[], long start, long mid, long end) {
				long p = start, q = mid + 1;
				long Arr[] = new long[(int) (end - start + 1)];
				long k = 0;

				for (int i = (int) start; i <= end; i++) {
					if (p > mid)
						Arr[(int) k++] = A[(int) q++];

					else if (q > end)
						Arr[(int) k++] = A[(int) p++];

					else if (A[(int) p] < A[(int) q])
						Arr[(int) k++] = A[(int) p++];

					else
						Arr[(int) k++] = A[(int) q++];
				}
				for (int i = 0; i < k; i++) {
					A[(int) start++] = Arr[i];
				}

			}

		}

	}


import java.util.Scanner;

public class Q2 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int[][] A = new int[N + 1][N + 1];

        int[] a = new int[N + 1];
        for (int i = 1; i <= N; ++i) {
            a[i] = scan.nextInt();
        }
        for (int i = 1; i <= N; ++i) {
            String s = scan.next();
            for (int j = 1; j <= N; ++j) {
                int in = Integer.parseInt(s.charAt(j-1)+"");
                A[i][j] = in;
            }
        }
        int changes = 1;
        while (changes != 0) {
            changes = 0;
            for (int i = 1; i <= N; ++i) {
                int smallestIndex = i;
                for (int j = i+1; j <= N; ++j) {
                    if (A[i][j]==1 && a[smallestIndex] > a[j]) {
                        smallestIndex = j;
                    }
                }
               if (smallestIndex>i) {
                    changes++;
                    int temp = a[i];
                    a[i] = a[smallestIndex];
                    a[smallestIndex] = temp;
                }
            }
        }
        for(int i=1;i<=N;++i)System.out.print(a[i]+" ");
    }
}


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class NewYearPermutation {

    public static void dfs(boolean[][] a, boolean skip[], int base, int next){
        if(!skip[next]){skip[next] = true;}else return;
        for(int j=next+1;j<=a.length-1;++j){
            if(a[next][j]){
                for(int k=1;k<=a.length-1;++k){
                    a[base][k] |= a[j][k];
                }
                dfs(a, skip, base, j);
            }
        }
    }

    public static void main(String[] args) throws Exception{
        int N = IO.readInt();
        int[] numbers = IO.readIntArray(N, " ");
        boolean[][] matrix = IO.read2dBooleanArray(N, N, "");
        boolean[] skip = new boolean[N+1];
        for(int i=1;i<=N/2+1;++i) {matrix[i][i] = true; dfs(matrix, skip, i, i);  }

        for(int i=1;i<=N/2+1;++i){
            ArrayList<Integer> a = new ArrayList<>();
            for(int j=1;j<=N;++j){
                if(matrix[i][j])a.add(numbers[j]);
            }
            Collections.sort(a);
            int k=0;
            for(int j=1;j<=N;++j){
                if(matrix[i][j])numbers[j] = a.get(k++);
            }
        }
        IO.printIntArray(numbers);
    }

    static class IO {
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public static int[][] read2dInt (int rows, int cols, String seperator) throws Exception{
            int[][] arr = new int[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readIntArray(cols, seperator);
            }
            return arr;
        }

        public static boolean[][] read2dBooleanArray(int rows, int cols, String seperator) throws IOException{
            boolean[][] arr = new boolean[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readBooleanArray(cols, seperator);
            }
            return arr;
        }

        public static int[] readIntArray(int nInts, String seperator) throws IOException{
            String ints = br.readLine();
            String[] sArray = ints.split(seperator);
            int[] array = new int[nInts+1];
            for(int i=1;i<=nInts;++i)array[i] = Integer.parseInt(sArray[i-1]);
            return array;
        }

        public static boolean[] readBooleanArray(int nBooleans, String seperator) throws IOException{
            String booleans = br.readLine();
            String[] sArray = booleans.split(seperator);
            boolean[] array = new boolean[nBooleans+1];
            for(int i=1;i<=nBooleans;++i){array[i] = sArray[i-1].matches("1");}
            return array;
        }

        public static long[] readLongArray(int nLongs, String seperator) throws IOException{
            String longs = br.readLine();
            String[] sArray = longs.split(seperator);
            long[] array = new long[nLongs+1];
            for(int i=1;i<=nLongs;++i)array[i] = Long.parseLong(sArray[i-1]);
            return array;
        }

        public static double[] readDoubleArray(int nDoubles, String seperator) throws IOException{
            String doubles = br.readLine();
            String[] sArray = doubles.split(seperator);
            double[] array = new double[nDoubles+1];
            for(int i=1;i<=nDoubles;++i)array[i] = Double.parseDouble(sArray[i-1]);
            return array;
        }

        public static char[] readCharArray(int nChars, String seperator) throws IOException{
            String chars = br.readLine();
            String[] sArray = chars.split(seperator);
            char[] array = new char[nChars+1];
            for(int i=1;i<=nChars;++i)array[i] = sArray[i-1].charAt(0);
            return array;
        }

        public static int readInt() throws IOException{
            String in = br.readLine();
            return Integer.parseInt(in);
        }

        public static double readDouble() throws IOException{
            String in = br.readLine();
            return Double.parseDouble(in);
        }

        public static long readLong() throws IOException{
            String in = br.readLine();
            return Long.parseLong(in);
        }

        public static int readChar() throws IOException{
            String in = br.readLine();
            return in.charAt(0);
        }

        public static void print(Object... o){
            for(Object os:o) System.out.print(os);
        }

        public static void println(Object... o){
            for(Object os:o) System.out.print(os);
            System.out.print("\n");
        }

        public static void printIntArray(int[] o){
            for(int i = 1;i<=o.length-1;++i)IO.print(o[i]+" ");
            IO.println();
        }
        public static void printlnSeperate(String seperator, Object... o){
            StringBuilder sb = new StringBuilder();
            sb.delete(sb.length()-seperator.length(), sb.length());
            System.out.println(sb);
        }
    }
}


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class NewYear2 {
    public static void dfs2(boolean[][] a, boolean skip[], int base, int next){
        if(!skip[next]){skip[next] = true;}else return;

        for(int j=next+1;j<=a.length-1;++j){
            if(a[next][j]){
                for(int k=1;k<=a.length-1;++k){
                    a[base][k] |= a[j][k];
                }
                dfs2(a, skip, base, j);
            }
        }
    }

    public static void main(String[] args) throws Exception{
        int N = IO.readInt();
        int[] numbers = IO.readIntArray(N, " ");
        boolean[][] matrix = IO.read2dBooleanArray(N, N, "");
        boolean[] skip = new boolean[N+1];
        for(int i=1;i<=N;++i)dfs2(matrix, skip, i, i);

        for(int i=1;i<=N;++i){
            ArrayList<Integer> a = new ArrayList<>();
            for(int j=1;j<=N;++j){
                if(matrix[i][j])a.add(numbers[j]);
            }
            Collections.sort(a);
            int k=0;
            for(int j=1;j<=N;++j){
                if(matrix[i][j])numbers[j] = a.get(k++);
            }
        }
        IO.printIntArray(numbers);
    }

    public static void printArray(boolean[][] a){
        for(int i=1;i<=a.length-1;++i){
            for(int j=1;j<=a.length-1;++j){
                if(a[i][j])IO.print(1+" ");
                else IO.print(0+" ");
            }IO.println();
        }
    }
    static class IO {
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public static int[][] read2dInt (int rows, int cols, String seperator) throws Exception{
            int[][] arr = new int[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readIntArray(cols, seperator);
            }
            return arr;
        }

        public static boolean[][] read2dBooleanArray(int rows, int cols, String seperator) throws IOException{
            boolean[][] arr = new boolean[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readBooleanArray(cols, seperator);
            }
            return arr;
        }

        public static int[] readIntArray(int nInts, String seperator) throws IOException{
            String ints = br.readLine();
            String[] sArray = ints.split(seperator);
            int[] array = new int[nInts+1];
            for(int i=1;i<=nInts;++i)array[i] = Integer.parseInt(sArray[i-1]);
            return array;
        }

        public static boolean[] readBooleanArray(int nBooleans, String seperator) throws IOException{
            String booleans = br.readLine();
            String[] sArray = booleans.split(seperator);
            boolean[] array = new boolean[nBooleans+1];
            for(int i=1;i<=nBooleans;++i){array[i] = sArray[i-1].matches("1");}
            return array;
        }

        public static long[] readLongArray(int nLongs, String seperator) throws IOException{
            String longs = br.readLine();
            String[] sArray = longs.split(seperator);
            long[] array = new long[nLongs+1];
            for(int i=1;i<=nLongs;++i)array[i] = Long.parseLong(sArray[i-1]);
            return array;
        }

        public static double[] readDoubleArray(int nDoubles, String seperator) throws IOException{
            String doubles = br.readLine();
            String[] sArray = doubles.split(seperator);
            double[] array = new double[nDoubles+1];
            for(int i=1;i<=nDoubles;++i)array[i] = Double.parseDouble(sArray[i-1]);
            return array;
        }

        public static char[] readCharArray(int nChars, String seperator) throws IOException{
            String chars = br.readLine();
            String[] sArray = chars.split(seperator);
            char[] array = new char[nChars+1];
            for(int i=1;i<=nChars;++i)array[i] = sArray[i-1].charAt(0);
            return array;
        }

        public static int readInt() throws IOException{
            String in = br.readLine();
            return Integer.parseInt(in);
        }

        public static double readDouble() throws IOException{
            String in = br.readLine();
            return Double.parseDouble(in);
        }

        public static long readLong() throws IOException{
            String in = br.readLine();
            return Long.parseLong(in);
        }

        public static int readChar() throws IOException{
            String in = br.readLine();
            return in.charAt(0);
        }

        public static void print(Object... o){
            for(Object os:o) System.out.print(os);
        }

        public static void println(Object... o){
            for(Object os:o) System.out.print(os);
            System.out.print("\n");
        }

        public static void printIntArray(int[] o){
            for(int i = 1;i<=o.length-1;++i)IO.print(o[i]+" ");
            IO.println();
        }
        public static void printlnSeperate(String seperator, Object... o){
            StringBuilder sb = new StringBuilder();
            sb.delete(sb.length()-seperator.length(), sb.length());
            System.out.println(sb);
        }
    }
}


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class NewYearPermutation {

    public static void dfs(boolean[][] a, boolean skip[], int base, int next){
        if(!skip[next]){skip[next] = true;}else return;
        for(int j=next+1;j<=a.length-1;++j){
            if(a[next][j]){
                for(int k=1;k<=a.length-1;++k){
                    a[base][k] |= a[j][k];
                }
                dfs(a, skip, base, j);
            }
        }
    }

    public static void main(String[] args) throws Exception{
        int N = IO.readInt();
        int[] numbers = IO.readIntArray(N, " ");
        boolean[][] matrix = IO.read2dBooleanArray(N, N, "");
        boolean[] skip = new boolean[N+1];
        for(int i=1;i<=N;++i) {dfs(matrix, skip, i, i);  }

        for(int i=1;i<=N;++i){
            ArrayList<Integer> a = new ArrayList<>();
            for(int j=i;j<=N;++j){
                if(matrix[i][j])a.add(numbers[j]);
            }
            Collections.sort(a);
            int k=0;
            for(int j=i;j<=N;++j){
                if(matrix[i][j])numbers[j] = a.get(k++);
            }
        }
        IO.printIntArray(numbers);
    }

    static class IO {
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public static int[][] read2dInt (int rows, int cols, String seperator) throws Exception{
            int[][] arr = new int[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readIntArray(cols, seperator);
            }
            return arr;
        }

        public static boolean[][] read2dBooleanArray(int rows, int cols, String seperator) throws IOException{
            boolean[][] arr = new boolean[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readBooleanArray(cols, seperator);
            }
            return arr;
        }

        public static int[] readIntArray(int nInts, String seperator) throws IOException{
            String ints = br.readLine();
            String[] sArray = ints.split(seperator);
            int[] array = new int[nInts+1];
            for(int i=1;i<=nInts;++i)array[i] = Integer.parseInt(sArray[i-1]);
            return array;
        }

        public static boolean[] readBooleanArray(int nBooleans, String seperator) throws IOException{
            String booleans = br.readLine();
            String[] sArray = booleans.split(seperator);
            boolean[] array = new boolean[nBooleans+1];
            for(int i=1;i<=nBooleans;++i){array[i] = sArray[i-1].matches("1");}
            return array;
        }

        public static long[] readLongArray(int nLongs, String seperator) throws IOException{
            String longs = br.readLine();
            String[] sArray = longs.split(seperator);
            long[] array = new long[nLongs+1];
            for(int i=1;i<=nLongs;++i)array[i] = Long.parseLong(sArray[i-1]);
            return array;
        }

        public static double[] readDoubleArray(int nDoubles, String seperator) throws IOException{
            String doubles = br.readLine();
            String[] sArray = doubles.split(seperator);
            double[] array = new double[nDoubles+1];
            for(int i=1;i<=nDoubles;++i)array[i] = Double.parseDouble(sArray[i-1]);
            return array;
        }

        public static char[] readCharArray(int nChars, String seperator) throws IOException{
            String chars = br.readLine();
            String[] sArray = chars.split(seperator);
            char[] array = new char[nChars+1];
            for(int i=1;i<=nChars;++i)array[i] = sArray[i-1].charAt(0);
            return array;
        }

        public static int readInt() throws IOException{
            String in = br.readLine();
            return Integer.parseInt(in);
        }

        public static double readDouble() throws IOException{
            String in = br.readLine();
            return Double.parseDouble(in);
        }

        public static long readLong() throws IOException{
            String in = br.readLine();
            return Long.parseLong(in);
        }

        public static int readChar() throws IOException{
            String in = br.readLine();
            return in.charAt(0);
        }

        public static void print(Object... o){
            for(Object os:o) System.out.print(os);
        }

        public static void println(Object... o){
            for(Object os:o) System.out.print(os);
            System.out.print("\n");
        }

        public static void printIntArray(int[] o){
            for(int i = 1;i<=o.length-1;++i)IO.print(o[i]+" ");
            IO.println();
        }
        public static void printlnSeperate(String seperator, Object... o){
            StringBuilder sb = new StringBuilder();
            sb.delete(sb.length()-seperator.length(), sb.length());
            System.out.println(sb);
        }
    }
}


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class NewYearPermutation {

    public static void dfs(boolean[][] a, boolean skip[], int base, int next){
        if(!skip[next]){skip[next] = true;}else return;
        for(int j=1;j<=a.length-1;++j){
            if(a[next][j]){
                for(int k=1;k<=a.length-1;++k){
                    a[base][k] |= a[j][k];
                }
                dfs(a, skip, base, j);
            }
        }
    }

    public static void main(String[] args) throws Exception{
        int N = IO.readInt();
        int[] numbers = IO.readIntArray(N, " ");
        boolean[][] matrix = IO.read2dBooleanArray(N, N, "");
        boolean[] skip = new boolean[N+1];

        for(int i=1;i<=N;++i) {dfs(matrix, skip, i, i);}

        for(int i=1;i<=N;++i){
            ArrayList<Integer> a = new ArrayList<>();
            for(int j=1;j<=N;++j){
                if(matrix[i][j])a.add(numbers[j]);
            }
            Collections.sort(a);
            int k=0;
            for(int j=1;j<=N;++j){
                if(matrix[i][j])numbers[j] = a.get(k++);
            }
        }
        IO.printIntArray(numbers);
    }

    static class IO {
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public static int[][] read2dInt (int rows, int cols, String seperator) throws Exception{
            int[][] arr = new int[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readIntArray(cols, seperator);
            }
            return arr;
        }

        public static boolean[][] read2dBooleanArray(int rows, int cols, String seperator) throws IOException{
            boolean[][] arr = new boolean[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readBooleanArray(cols, seperator);
            }
            return arr;
        }

        public static int[] readIntArray(int nInts, String seperator) throws IOException{
            String ints = br.readLine();
            String[] sArray = ints.split(seperator);
            int[] array = new int[nInts+1];
            for(int i=1;i<=nInts;++i)array[i] = Integer.parseInt(sArray[i-1]);
            return array;
        }

        public static boolean[] readBooleanArray(int nBooleans, String seperator) throws IOException{
            String booleans = br.readLine();
            String[] sArray = booleans.split(seperator);
            boolean[] array = new boolean[nBooleans+1];
            for(int i=1;i<=nBooleans;++i){array[i] = sArray[i-1].matches("1");}
            return array;
        }

        public static long[] readLongArray(int nLongs, String seperator) throws IOException{
            String longs = br.readLine();
            String[] sArray = longs.split(seperator);
            long[] array = new long[nLongs+1];
            for(int i=1;i<=nLongs;++i)array[i] = Long.parseLong(sArray[i-1]);
            return array;
        }

        public static double[] readDoubleArray(int nDoubles, String seperator) throws IOException{
            String doubles = br.readLine();
            String[] sArray = doubles.split(seperator);
            double[] array = new double[nDoubles+1];
            for(int i=1;i<=nDoubles;++i)array[i] = Double.parseDouble(sArray[i-1]);
            return array;
        }

        public static char[] readCharArray(int nChars, String seperator) throws IOException{
            String chars = br.readLine();
            String[] sArray = chars.split(seperator);
            char[] array = new char[nChars+1];
            for(int i=1;i<=nChars;++i)array[i] = sArray[i-1].charAt(0);
            return array;
        }

        public static int readInt() throws IOException{
            String in = br.readLine();
            return Integer.parseInt(in);
        }

        public static double readDouble() throws IOException{
            String in = br.readLine();
            return Double.parseDouble(in);
        }

        public static long readLong() throws IOException{
            String in = br.readLine();
            return Long.parseLong(in);
        }

        public static int readChar() throws IOException{
            String in = br.readLine();
            return in.charAt(0);
        }

        public static void print(Object... o){
            for(Object os:o) System.out.print(os);
        }

        public static void println(Object... o){
            for(Object os:o) System.out.print(os);
            System.out.print("\n");
        }

        public static void printIntArray(int[] o){
            for(int i = 1;i<=o.length-1;++i)IO.print(o[i]+" ");
            IO.println();
        }

        public static void printlnSeperate(String seperator, Object... o){
            StringBuilder sb = new StringBuilder();
            sb.delete(sb.length()-seperator.length(), sb.length());
            System.out.println(sb);
        }
    }
}


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class NewYearPermutation {

    public static void dfs(boolean[][] a, boolean skip[], int base, int next){
        if(!skip[next]){skip[next] = true;}else return;
        for(int j=1;j<=a.length-1;++j){
            if(a[next][j]){
                for(int k=1;k<=a.length-1;++k){
                    a[base][k] |= a[j][k];
                }
                dfs(a, skip, base, j);
            }
        }
    }

    public static void main(String[] args) throws Exception{
        int N = IO.readInt();
        int[] numbers = IO.readIntArray(N, " ");
        boolean[][] matrix = IO.read2dBooleanArray(N, N, "");
        boolean[] skip = new boolean[N+1];
        for(int i=1;i<=N;++i)  dfs(matrix, skip, i, i);

        for(int i=1;i<=N;++i){
            ArrayList<Integer> a = new ArrayList<>();
            for(int j=1;j<=N;++j){
                if(matrix[i][j])a.add(numbers[j]);
            }
            Collections.sort(a);
            int k=0;
            for(int j=1;j<=N;++j){
                if(matrix[i][j])numbers[j] = a.get(k++);
            }
        }
        IO.printIntArray(numbers);
    }

    static class IO {
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public static int[][] read2dInt (int rows, int cols, String seperator) throws Exception{
            int[][] arr = new int[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readIntArray(cols, seperator);
            }
            return arr;
        }

        public static boolean[][] read2dBooleanArray(int rows, int cols, String seperator) throws IOException{
            boolean[][] arr = new boolean[rows+1][cols+1];
            for(int i=1;i<=rows;++i){
                arr[i] = readBooleanArray(cols, seperator);
            }
            return arr;
        }

        public static int[] readIntArray(int nInts, String seperator) throws IOException{
            String ints = br.readLine();
            String[] sArray = ints.split(seperator);
            int[] array = new int[nInts+1];
            for(int i=1;i<=nInts;++i)array[i] = Integer.parseInt(sArray[i-1]);
            return array;
        }

        public static boolean[] readBooleanArray(int nBooleans, String seperator) throws IOException{
            String booleans = br.readLine();
            String[] sArray = booleans.split(seperator);
            boolean[] array = new boolean[nBooleans+1];
            for(int i=1;i<=nBooleans;++i){array[i] = sArray[i-1].matches("1");}
            return array;
        }

        public static long[] readLongArray(int nLongs, String seperator) throws IOException{
            String longs = br.readLine();
            String[] sArray = longs.split(seperator);
            long[] array = new long[nLongs+1];
            for(int i=1;i<=nLongs;++i)array[i] = Long.parseLong(sArray[i-1]);
            return array;
        }

        public static double[] readDoubleArray(int nDoubles, String seperator) throws IOException{
            String doubles = br.readLine();
            String[] sArray = doubles.split(seperator);
            double[] array = new double[nDoubles+1];
            for(int i=1;i<=nDoubles;++i)array[i] = Double.parseDouble(sArray[i-1]);
            return array;
        }

        public static char[] readCharArray(int nChars, String seperator) throws IOException{
            String chars = br.readLine();
            String[] sArray = chars.split(seperator);
            char[] array = new char[nChars+1];
            for(int i=1;i<=nChars;++i)array[i] = sArray[i-1].charAt(0);
            return array;
        }

        public static int readInt() throws IOException{
            String in = br.readLine();
            return Integer.parseInt(in);
        }

        public static double readDouble() throws IOException{
            String in = br.readLine();
            return Double.parseDouble(in);
        }

        public static long readLong() throws IOException{
            String in = br.readLine();
            return Long.parseLong(in);
        }

        public static int readChar() throws IOException{
            String in = br.readLine();
            return in.charAt(0);
        }

        public static void print(Object... o){
            for(Object os:o) System.out.print(os);
        }

        public static void println(Object... o){
            for(Object os:o) System.out.print(os);
            System.out.print("\n");
        }

        public static void printIntArray(int[] o){
            for(int i = 1;i<=o.length-1;++i)IO.print(o[i]+" ");
            IO.println();
        }
        public static void printlnSeperate(String seperator, Object... o){
            StringBuilder sb = new StringBuilder();
            sb.delete(sb.length()-seperator.length(), sb.length());
            System.out.println(sb);
        }
    }
}