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=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=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;++i)  dfs(matrix, skip, i, i);
        int good = N/2;

        for(int i=1;i<=good;++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=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;++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 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) {matrix[i][i] = true;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);
    }

    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.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 = 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, int base, int next){

        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, 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, "");
        for(int i=1;i<=N;++i)  dfs(matrix, i, i);
        int good = N/2;

        for(int i=1;i<=good;++i){
            ArrayList<Integer> a = new ArrayList<>();
            for(int j=1;j<=N;++j){
                if(matrix[i][j])a.add(numbers[j]);
            }
            Collections.sort(a);
            for(int j=1;j<=N;++j){
                if(matrix[i][j])numbers[j] = a.remove(0);
            }
        }
        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=next+1;j<=a.length-1;++j){
            if(a[next][j]){
                for(int k=next+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;++i) {matrix[i][i] = true; dfs(matrix, skip, i, i);  }

        for(int i=1;i<=N/2;++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=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);
        int good = N/2;

        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.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = in.nextAi(n);
        char[][] m = in.nextCMatrix(n);
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                int pos = -1;
                int min = a[i];
                for (int j = i + 1; j < n; j++) {
                    if (m[i][j] == '1') {
                        if (a[j] < min) {
                            min = a[j];
                            pos = j;
                        }
                    }
                }
                if (pos != -1) {
                    int t = a[i];
                    a[i] = a[pos];
                    a[pos] = t;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            out.print(a[i] + " ");
        }

    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

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

    public char[][] nextCMatrix(int n) {
        char[][] c = new char[n][];
        for (int i = 0; i < n; i++) {
            c[i] = next().toCharArray();
        }
        return c;
    }

}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = in.nextAi(n);
        char[][] m = in.nextCMatrix(n);

        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (m[i][k] == '1' && m[k][j] == '1') {
                        m[i][j] = m[j][i] = '1';
                    }
                }
            }
        }

        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                int pos = -1;
                int min = a[i];
                for (int j = i + 1; j < n; j++) {
                    if (m[i][j] == '1') {
                        if (a[j] < min) {
                            min = a[j];
                            pos = j;
                        }
                    }
                }
                if (pos != -1) {
                    int t = a[i];
                    a[i] = a[pos];
                    a[pos] = t;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            out.print(a[i] + " ");
        }

    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

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

    public char[][] nextCMatrix(int n) {
        char[][] c = new char[n][];
        for (int i = 0; i < n; i++) {
            c[i] = next().toCharArray();
        }
        return c;
    }

}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = in.nextAi(n);
        char[][] m = in.nextCMatrix(n);

        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (m[i][k] == '1' && m[k][j] == '1') {
                        m[i][j] = m[j][i] = 1;
                    }
                }
            }
        }

        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                int pos = -1;
                int min = a[i];
                for (int j = i + 1; j < n; j++) {
                    if (m[i][j] == '1') {
                        if (a[j] < min) {
                            min = a[j];
                            pos = j;
                        }
                    }
                }
                if (pos != -1) {
                    int t = a[i];
                    a[i] = a[pos];
                    a[pos] = t;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            out.print(a[i] + " ");
        }

    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

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

    public char[][] nextCMatrix(int n) {
        char[][] c = new char[n][];
        for (int i = 0; i < n; i++) {
            c[i] = next().toCharArray();
        }
        return c;
    }

}


import java.util.*;

public class NewClass {
    public static void main(String[] args){
        Scanner read=new Scanner(System.in);
        int n=read.nextInt();

        int[] arr=new int [n];
        for (int i=0 ; i<n  ; i++)
            arr[i]=read.nextInt();

        int [][] adj=new int [n][n];
        Stack <Integer> s[]=(Stack<Integer>[]) new Stack[n];

        for (int i=0 ; i<n ; i++){
            s[i]=new Stack<Integer>();
            String ss=read.next();
            for (int j=0 ; j<n ; j++){
                if (ss.charAt(j)=='1')
                    s[i].push(j);
            }
        }

        for (int i=0 ; i< n ; i++){
            int min=arr[i];
            int minind=i;
            boolean [] seen=new boolean [n];
            for (int j=0 ; j<n ; j++)
                seen[j]=false;

            seen[i]=true;

            Queue<Integer> q=new LinkedList<Integer>();

            q.add(i);

            while(q.size()!=0){
                int x=q.peek();
              //  System.out.println(x);
                for (int j=0 ; j<s[x].size() ; j++){
                    if (!seen[s[x].elementAt(j)]){
                        q.add(s[x].elementAt(j));
                        seen[s[x].elementAt(j)]=true;
                        if(arr[s[x].elementAt(j)]<min && i< s[x].elementAt(j)){
                            min=arr[s[x].elementAt(j)];
                            minind=s[x].elementAt(j);
                        }
                    }
                }
                q.remove();
            }
        //    System.out.println(min+" "+minind);
            int tmp=arr[i];
            arr[i]=arr[minind];
            arr[minind]=tmp;

        }
        for (int i=0 ; i<n ; i++)
            System.out.print(arr[i]+" ");
    }
}


import java.util.*;

public class NewClass {
    public static void main(String[] args){
        Scanner read=new Scanner(System.in);
        int n=read.nextInt();

        int[] arr=new int [n];
        for (int i=0 ; i<n  ; i++)
            arr[i]=read.nextInt();

        int [][] adj=new int [n][n];
        Stack <Integer> s[]=(Stack<Integer>[]) new Stack[n];

        for (int i=0 ; i<n ; i++){
            s[i]=new Stack<Integer>();
            String ss=read.next();
            for (int j=0 ; j<n ; j++){
                if (ss.charAt(j)=='1')
                    s[i].push(j);
            }
        }

        for (int i=0 ; i< n ; i++){
            int min=arr[i];
            int minind=i;
            boolean [] seen=new boolean [n];
            for (int j=0 ; j<n ; j++)
                seen[j]=false;

            seen[0]=true;

            Queue<Integer> q=new LinkedList<Integer>();

            q.add(i);

            while(q.size()!=0){
                int x=q.peek();
              //  System.out.println(x);
                for (int j=0 ; j<s[x].size() ; j++){
                    if (!seen[s[x].elementAt(j)]){
                        q.add(s[x].elementAt(j));
                        seen[s[x].elementAt(j)]=true;
                        if(arr[s[x].elementAt(j)]<min && i< s[x].elementAt(j)){
                            min=arr[s[x].elementAt(j)];
                            minind=s[x].elementAt(j);
                        }
                    }
                }
                q.remove();
            }
            //System.out.println(min+" "+minind);
            int tmp=arr[i];
            arr[i]=arr[minind];
            arr[minind]=tmp;

        }
        for (int i=0 ; i<n ; i++)
            System.out.print(arr[i]+" ");
    }
}


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

public class B {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
		Scanner s = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = Integer.parseInt(f.readLine());
		String[] split = f.readLine().split("\\s+");
		int[] a = new int[n];
		int[] pos = new int[n+1];
		for(int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(split[i]);
			pos[a[i]] = i;
		}
		ArrayList<Integer>[] getTo = new ArrayList[n];
		for(int i = 0; i < n; i++) getTo[i] = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			char[] b = f.readLine().toCharArray();
			for(int j = 0; j < n; j++) {
				if(b[j] == '1') getTo[i].add(j);
			}
		}
		for(int i = 1; i <= n; i++) {
			int at = pos[i];
			boolean swapped = true;
			while(swapped) {
				//look for an optimal place to swap to.
				swapped = false;
				for(Integer j : getTo[at]) {
					if(a[j] > a[at] && j < at) {
						swapped = true;
						int temp = a[j];
						a[j] = a[at];
						a[at] = temp;
						pos[i] = j;
						pos[temp] = at;
						at = j;
						break;
					} else if(a[j] < a[at] && j > at) {
						swapped = true;
						int temp = a[j];
						a[j] = a[at];
						a[at] = temp;
						pos[i] = j;
						pos[temp] = at;
						at = j;
						break;
					}
				}
			}
		}
		for(int i = 0; i < n; i++) out.print(a[i] + " ");
		out.println();
		out.close();
	}
}


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

public class B {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
		Scanner s = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = Integer.parseInt(f.readLine());
		String[] split = f.readLine().split("\\s+");
		int[] a = new int[n];
		int[] pos = new int[n+1];
		for(int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(split[i]);
			pos[a[i]] = i;
		}
		ArrayList<Integer>[] getTo = new ArrayList[n];
		for(int i = 0; i < n; i++) getTo[i] = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			char[] b = f.readLine().toCharArray();
			for(int j = 0; j < n; j++) {
				if(b[j] == '1') getTo[i].add(j);
			}
		}
		boolean[] V = new boolean[n];
		for(int i = 0; i < n; i++) {
			if(!V[i]) {
				V[i] = true;
				TreeSet<Integer> p = new TreeSet<>(), vals = new TreeSet<>();
				dfs(a, i, V, getTo, p, vals);
				while(!p.isEmpty()) {
					a[p.pollFirst()] = vals.pollFirst();
				}
			}
		}
		for(int i = 0; i < n; i++) out.print(a[i] + " ");
		out.println();
		out.close();
	}

	static void dfs(int[] a, int at, boolean[] V, ArrayList<Integer>[] adj, TreeSet<Integer> pos, TreeSet<Integer> vals) {
		vals.add(a[at]);
		pos.add(at);
		for(Integer i : adj[at]) {
			if(!V[i]) {
				V[i] = true;
				dfs(a, i, V, adj, pos, vals);
			}
		}
	}
}


import java.util.*;
import java.io.*;
public final class Main{
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		int[][] s=new int[n][n];
		for(int i=0;i<n;i++)
			a[i]=in.nextInt();
		for(int i=0;i<n;i++){
			String str=in.next();
			for(int j=0;j<n;j++)
				s[i][j]=Integer.parseInt(str.substring(j,j+1));
		}
		for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			int min=a[i];
			int pos=i;
			for(int j=i+1;j<n;j++){
				if(s[i][j]==1 && a[j]<min){
					min=a[j];
					pos=j;
				}
			}
			a[pos]=a[i];
			a[i]=min;
		}
		}
		for(int k=0;k<n;k++)
			System.out.print(a[k]+" ");
		System.out.println();

	}
}


import java.util.*;
import java.io.*;
public final class Main{
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		int[][] s=new int[n][n];
		for(int i=0;i<n;i++)
			a[i]=in.nextInt();
		for(int i=0;i<n;i++){
			String str=in.next();
			for(int j=0;j<n;j++)
				s[i][j]=Integer.parseInt(str.substring(j,j+1));
		}
		for(int k=0;k<2*n;k++){
			for(int i=0;i<n;i++){
				for(int j=i+1;j<n;j++){
					if(s[i][j]==1 && a[j]<a[i]){
						int min=a[j];
						a[j]=a[i];
						a[i]=min;
					}
				}
			}
		}
		for(int k=0;k<n;k++)
			System.out.print(a[k]+" ");
	}
}


import java.util.*;
import java.io.*;
public final class Main{
	LinkedList<Integer> adj[];
	int V;
	Main(int n){
		V=n;
		adj=new LinkedList[V];
		for(int i=0;i<V;i++)
			adj[i]=new LinkedList<>();
	}
	void addEdge(int u,int v){
		adj[u].add(v);
		adj[v].add(u);
	}
	void dfs(int[] a){
		boolean[] visited=new boolean[V];
		for(int i=0;i<V;i++){
			if(visited[i]==false){
				LinkedList<Integer> arli=new LinkedList<>();
				LinkedList<Integer> pos=new LinkedList<Integer>();
				dfsutil(visited,arli,pos,a,i);
				Collections.sort(arli);
				Collections.sort(pos);
				for(int j=0;j<pos.size();j++)
					a[pos.get(j)]=arli.get(j);
			}
		}
		for(int i=0;i<V;i++)
			System.out.print(a[i]+" ");
	}
	void dfsutil(boolean[] visited,LinkedList<Integer> arli,LinkedList<Integer> pos,int[] a,int i){
		arli.add(a[i]);
		pos.add(i);
		visited[i]=true;
		for(int j=0;j<adj[i].size();j++){
			int x=adj[i].get(j);
			if(visited[x]==false)
				dfsutil(visited,arli,pos,a,x);
		}
	}
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		Main ob=new Main(n);
		for(int i=0;i<n;i++)
			a[i]=in.nextInt();
		for(int i=0;i<n;i++){
			String str=in.next();
			for(int j=0;j<n;j++){
				if(str.charAt(j)=='1')
					ob.addEdge(i, j);
			}
		}
		ob.dfs(a);
	}
}


import java.util.*;
import java.io.*;
public final class Main{
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		int[][] s=new int[n][n];
		for(int i=0;i<n;i++)
			a[i]=in.nextInt();
		for(int i=0;i<n;i++){
			String str=in.next();
			for(int j=0;j<n;j++)
				s[i][j]=Integer.parseInt(str.substring(j,j+1));
		}
		for(int k=0;k<n+1;k++){
		for(int i=0;i<n;i++){
			int min=a[i];
			int pos=i;
			for(int j=i+1;j<n;j++){
				if(s[i][j]==1 && a[j]<min){
					min=a[j];
					pos=j;
				}
			}
			a[pos]=a[i];
			a[i]=min;
		}
		}
		for(int k=0;k<n;k++)
			System.out.print(a[k]+" ");
		System.out.println();

	}
}


import java.util.*;
import java.io.*;
public final class Main{
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		int[][] s=new int[n][n];
		for(int i=0;i<n;i++)
			a[i]=in.nextInt();
		for(int i=0;i<n;i++){
			String str=in.next();
			for(int j=0;j<n;j++)
				s[i][j]=Integer.parseInt(str.substring(j,j+1));
		}
		for(int k=0;k<2*n;k++){
			for(int i=0;i<n;i++){
				int min=a[i];
				int pos=i;
				for(int j=i+1;j<n;j++){
					if(s[i][j]==1 && a[j]<min){
						min=a[j];
						pos=j;
					}
				}
				a[pos]=a[i];
				a[i]=min;
			}
		}
		for(int k=0;k<n;k++)
			System.out.print(a[k]+" ");
	}
}


import java.io.*;
import java.util.*;
public class newYearPermutation {
	private static void DFS(int loc, int[] visited, int[][] con, int count) {
		visited[loc] = count;
		for (int x = 0; x < con.length; x++) {
			if (con[loc][x] ==1 && visited[x]==0) {
				DFS(x,visited,con,count);
			}
		}
	}
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int quant = Integer.parseInt(in.readLine());
		StringTokenizer s1 = new StringTokenizer(in.readLine());
		int[] arr = new int[quant];
		for (int x = 0; x < quant; x++) {
			arr[x] = Integer.parseInt(s1.nextToken());
		}
		int[][] con = new int[quant][quant];
		for (int x = 0; x < quant; x++) {
			String s = in.readLine();
			for (int y = 0; y < quant; y++) {
				con[x][y] = s.charAt(y)-'0';
			}
		}
		int counter = 1;
		int[] visited = new int[quant];
		for (int x = 0; x < visited.length; x++) {
			if (visited[x] == 0) {
				DFS(x, visited, con, counter);
				counter++;
			}
		}
		int[] res = new int[quant];
		for (int x = 1; x <= quant; x++) {
			PriorityQueue<Integer> set = new PriorityQueue<Integer>();
			for (int y = 0; y < quant; y++) {
				if (visited[y] == x) {
					set.add(arr[y]);
				}
			}
			for (int y = 0; y < quant; y++) {
				if (visited[y] == x) {
					res[y] = set.remove();
				}
			}
		}
		for (int x= 0; x < quant-1; x++) {
			System.out.print(res[x] + " ");
		}
		System.out.println(res[quant-1]);
	}
}


import java.io.*;
import java.util.*;
public class newYearPermutation {
	private static int[] min;
	private static void DFS(int[] cur, boolean[][] conn) {
		//System.out.println(cur[0]);
		boolean changed = false;
		for (int x = 0; x < conn.length; x++) {
			for (int y = x+1; y < conn.length; y++) {
				if (conn[x][y] && cur[x] > cur[y]) {
					int temp = cur[x];
					cur[x] = cur[y];
					cur[y] = temp;
					DFS(cur, conn);
					temp = cur[x];
					cur[x] = cur[y];
					cur[y] = temp;
					changed = true;
				}
			}
		}
		if (!changed) {
			for (int x = 0; x < cur.length; x++) {
				min[x] = cur[x];
			}
		}
	}
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int quant = Integer.parseInt(in.readLine());
		int[] nums = new int[quant];
		StringTokenizer s1 = new StringTokenizer(in.readLine());
		min = new int[quant];
		for (int x = 0; x < nums.length; x++) {
			nums[x] = Integer.parseInt(s1.nextToken());
		}
		boolean[][] conn = new boolean[quant][quant];
		for (int x = 0; x < quant; x++) {
			String rd = in.readLine();
			for (int y = 0; y < quant; y++) {
				conn[x][y] = rd.charAt(y) == '1';
			}
		}
		DFS(nums, conn);
		for (int x = 0; x < min.length-1; x++) {
			System.out.print(min[x]+ " ");
		}
		System.out.println(min[min.length-1]);
	}
}


import java.util.Arrays;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int a[] = new int[n + 1];
        for(int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        boolean b[][] = new boolean[n + 1][n + 1];
        for(int i = 0; i < n; i++) {
            String s = in.next();
            for(int j = 0; j  < s.length(); j++) {
                b[i][j] = s.charAt(j) == '1' ? true : false;
            }
        }

        for(int i = 0; i < n; i++) {
            for(int j = 0; j  < n; j++) {
                if(b[i][j]) {
                    get(a, b, i, j);
                }
            }
        }

        int c[] = new int[n + 1];
        int count;
        for(int i = 0; i < n; i++) {
            Arrays.fill(c, 999);
            count = 0;
            c[count++] = a[i];
            for(int j = i + 1; j < n; j++) {
                if(b[i][j]) {
                    c[count++] = a[j];
                }
            }
            Arrays.sort(c);
            count = 0;
            a[i] = c[count++];
            for(int j = i + 1; j < n; j++) {
                if(b[i][j]) {
                    a[j] = c[count++];
                }
            }
            out.print(a[i] + " ");
        }

    }

    void get(int a[], boolean b[][], int i, int j) {
        for(int q = i + 1; q < a.length; q++) {
            if(b[j][q] && !b[i][q] && q != i && q != j) {
                b[i][q] = true;
                get(a, b, i, q);
                get(a, b, j, q);
            }
        }
    }
}

class InputReader {

    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

}


import java.util.Arrays;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int a[] = new int[n + 1];
        for(int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        boolean b[][] = new boolean[n + 1][n + 1];
        for(int i = 0; i < n; i++) {
            String s = in.next();
            for(int j = 0; j  < s.length(); j++) {
                b[i][j] = s.charAt(j) == '1' ? true : false;
            }
        }

        int c[] = new int[n + 1];
        int count;
        for(int i = 0; i < n; i++) {
            Arrays.fill(c, 999);
            count = 0;
            c[count++] = a[i];
            for(int j = i + 1; j < n; j++) {
                if(b[i][j]) {
                    c[count++] = a[j];
                }
            }
            Arrays.sort(c);
            count = 0;
            a[i] = c[count++];
            for(int j = i + 1; j < n; j++) {
                if(b[i][j]) {
                    a[j] = c[count++];
                }
            }
            out.print(a[i] + " ");
        }

    }
}

class InputReader {

    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

}


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

public class gfg {

    //static int n =0;
    public static void main(String[] args) throws IOException{
        Reader.init(System.in);
        //PrintWriter out = new PrintWriter(System.out);

        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        //out.close();
    }

    static TreeSet<Integer>[] graph;
    static int n=0;
    static int m=0;
    static boolean[] vis;
    static TreeSet<Integer> set= new TreeSet<>();
    static int cnt=0;
    static long mod =1000000007;
    static int[] arr;
    static PriorityQueue<Integer> ind = new PriorityQueue<>();
    static PriorityQueue<Integer> val = new PriorityQueue<>();

    static void solve()throws IOException{
        n = Reader.nextInt();
        graph = new TreeSet[n];
        arr  = new int[n];
        for(int i=0;i<n;i++){
            graph[i] = new TreeSet<Integer>();
        }
        for(int i=0;i<n;i++){
            arr[i] = Reader.nextInt();
        }
        vis = new boolean[n];

        for(int i=0;i<n;i++){
            String s = Reader.next();
            char[] c = s.toCharArray();
            for(int j=0;j<n;j++){
                if(c[j]=='1')
                {
                    graph[i].add(j);
                    graph[j].add(j);
                }
            }
        }
        for(int i=0;i<n;i++){
            if(!vis[i]){
                dfs(i);
                while(ind.size()>0){
                    int a = ind.poll();
                    int v = val.poll();
                    arr[a]=v;
                }
            }

        }
        for(int i=0;i<n;i++){
            System.out.print(arr[i]+" ");
        }
        return;

    }

    static void dfs(int a){
        vis[a]=true;
        ind.add(a);
        val.add(arr[a]);
        for(int x:graph[a]){
            if(!vis[x])
                dfs(x);
        }
    }

    // ggratest common divisor
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    }

    // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    }

    // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    }

}

class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}
class newcomparator implements Comparator<Integer>{
    //@Override
    public int compare(Integer a, Integer  b){
        return a<=b?1:-1;
    }
 }
class node {
    int a;
    long b;// cost to rreach\
    node(int s,long va){
        a=s;
        b=va;
    }
}
class mergesort{
    static void sort(int l, int h, int[] arr){
        if(l<h){
            int mid = (l+h)/2;

            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        }
    }
    static void merge(int l, int m , int h , int [] arr){

        int[] left = new int[m-l+1];
        int[] right = new int[h-m];
        for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        }
        //now left and right arrays are assumed to be sorted and we have tp merge them together
        // int the original aaray.
        int i=l;
        int lindex = 0;
        int rindex =  0;

        while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        }
        while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        }
        while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        }
    }

}


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

public class B500 {
    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 Set<Integer> done;

    static int[] arr;
    static char[][] mat;

    public static void main(String[] __) {
        FastReader sc = new FastReader();
        int n = sc.nextInt();
        arr = new int[n];
        mat = new char[n][n];
        for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
        for (int i = 0; i < n; i++) {
            mat[i] = sc.next().toCharArray();
        }
        done = new HashSet<>();
        for (int i = 0; i < n; i++) {
            int best = getBest(i);
            if (best != i) {
                arr[i] ^= arr[best];
                arr[best] ^= arr[i];
                arr[i] ^= arr[best];
            }
        }
        PrintWriter pw = new PrintWriter(System.out);
        for (int x : arr) pw.print(x + " ");
        pw.close();
    }

    private static int getBest(int idx) {
        int bestIdx = idx;
        Set<Integer> visited = new HashSet<>();
        List<Integer> next = new ArrayList<>();
        for (int i = 0; i < mat.length; i++) {
            if (mat[idx][i] == '1') next.add(i);
        }
        while (next.size() != 0) {
            List<Integer> neNex = new ArrayList<>();
            for (int x : next) {
                if (!visited.contains(x)) {
                    visited.add(x);
                    if (x >= idx) {
                        if (arr[x] < arr[bestIdx]) bestIdx = x;
                    }
                    for (int i = 0; i < mat.length; i++) {
                        if (mat[x][i] == '1') neNex.add(i);
                    }
                }
            }
            next = new ArrayList<>(neNex);
        }
        return bestIdx;
    }
}


                                                                                import java.util.*;
import java.io.*;
import java.math.*;

    public  class Sample implements Runnable
    {

           public static void solve()
          {
             int n=i();
             char[][] a=new char[n][n];
             int[] lol=new int[n];
             int min=-1;
             for(int i=0;i<n;i++)lol[i]=i();
             for(int i=0;i<n;i++)
             {

               String s=s();
               a[i]=s.toCharArray();
             }

             for(int m=0;m<n*n;m++)
             {
                 for(int i=0;i<n;i++)
             {
                min=i;
                for(int j=i;j<n;j++)
                {

                  if(a[i][j]=='1')
                  {
                    if(lol[j]<lol[min])
                    {
                      min=j;

                    }
                  }
                }
                 //out.print(lol[min]);
                int temp=lol[i];
                lol[i]=lol[min];
                lol[min]=temp;
             }
             }
             for(int i=0;i<n;i++)out.print(lol[i]+" ");
          }
          public void run()
          {
              solve();
              out.close();
          }

          public static void main(String[] args) throws IOException
          {
                new Thread(null, new Sample(), "whatever", 1<<26).start();
          }
         static class Pair implements Comparable<Pair>
         {
            long a;
            int b;
            Pair(){}
            Pair(long a,int b)
            {
                       this.a=a;
                       this.b=b;
            }

            public int compareTo(Pair x)
           {
                return Long.compare(x.a,this.a);
           }
        }

    ////////////////////////////////////////////////////// Merge Sort ////////////////////////////////////////////////////////////////////////

   static class Merge
   {

        public static void sort(long inputArr[])
        {
            int length = inputArr.length;
            doMergeSort(inputArr,0, length - 1);
        }

        private static void doMergeSort(long[] arr,int lowerIndex, int higherIndex)
        {
            if (lowerIndex < higherIndex) {
                int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
                doMergeSort(arr,lowerIndex, middle);
                doMergeSort(arr,middle + 1, higherIndex);
                mergeParts(arr,lowerIndex, middle, higherIndex);
            }
        }

        private static void mergeParts(long[]array,int lowerIndex, int middle, int higherIndex)
        {
            long[] temp=new long[higherIndex-lowerIndex+1];
            for (int i = lowerIndex; i <= higherIndex; i++)
            {
                temp[i-lowerIndex] = array[i];
            }
            int i = lowerIndex;
            int j = middle + 1;
            int k = lowerIndex;
            while (i <= middle && j <= higherIndex)
            {
                if (temp[i-lowerIndex] < temp[j-lowerIndex])
                {
                    array[k] = temp[i-lowerIndex];
                    i++;
                } else {
                    array[k] = temp[j-lowerIndex];
                    j++;
                }
                k++;
            }
            while (i <= middle)
            {
                array[k] = temp[i-lowerIndex];
                k++;
                i++;
            }
            while(j<=higherIndex)
            {
                array[k]=temp[j-lowerIndex];
                k++;
                j++;
            }
        }

    }

    /////////////////////////////////////////////////////////// Methods ////////////////////////////////////////////////////////////////////////

     static boolean isPal(String s)
     {
        for(int i=0, j=s.length()-1;i<=j;i++,j--)
        {
                if(s.charAt(i)!=s.charAt(j)) return false;
        }
        return true;
     }
     static String rev(String s)
     {
                StringBuilder sb=new StringBuilder(s);
                sb.reverse();
                return sb.toString();
     }
     static long gcd(long a,long b){return (a==0)?b:gcd(b%a,a);}
     static long gcdExtended(long a,long b,long[] x)
     {

        if(a==0){
            x[0]=0;
            x[1]=1;
            return b;
        }
        long[] y=new long[2];
        long gcd=gcdExtended(b%a, a, y);

        x[0]=y[1]-(b/a)*y[0];
        x[1]=y[0];

        return gcd;
    }

     boolean findSum(int set[], int n, long sum)
    {
      if (sum == 0)
         return true;
      if (n == 0 && sum != 0)
         return false;
      if (set[n-1] > sum)
         return findSum(set, n-1, sum);
      return findSum(set, n-1, sum) ||findSum(set, n-1, sum-set[n-1]);
    }

      public static long modPow(long base, long exp, long mod)
     {
        base = base % mod;
        long result = 1;
        while (exp > 0)
       {
            if (exp % 2 == 1)
            {
                result = (result * base) % mod;
            }
            base = (base * base) % mod;
            exp = exp >> 1;
       }
        return result;
    }

    static long[] fac;
    static long[] inv;
    static long mod=(long)1e9+7;
    public static void cal()
    {
        fac = new long[1000005];
        inv = new long[1000005];
        fac[0] = 1;
        inv[0] = 1;
        for (int i = 1; i <= 1000000; i++)
        {
            fac[i] = (fac[i - 1] * i) % mod;
            inv[i] = (inv[i - 1] * modPow(i, mod - 2, mod)) % mod;
        }
    }

    public static long ncr(int n, int r)
    {
        return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod;
    }

////////////////////////////////////////// Input Reader ////////////////////////////////////////////////////////////////////////////////////////////////////

    static InputReader sc = new InputReader(System.in);
    static PrintWriter out= new PrintWriter(System.out);

    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;
          }

          public long[] nextLongArray(int n)
          {
                  long a[] = new long[n];
                  for (int i = 0; i < n; i++)
                  {
                          a[i] = nextLong();
                  }
                  return a;
          }

          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 int i()
    {
        return sc.nextInt();
    }
    static long l(){
        return sc.nextLong();
    }
    static int[] iarr(int n)
    {
        return sc.nextIntArray(n);
    }
    static long[] larr(int n)
    {
        return sc.nextLongArray(n);
    }
    static String s(){
        return sc.nextLine();
    }

 }


                                                                                import java.util.*;
import java.io.*;
import java.math.*;

    public  class Sample implements Runnable
    {
          static ArrayList[] al;
          static int[] a;
          static boolean[] vis;
          static boolean[] vis1;
          static ArrayList arr;
          public static void solve()
          {
             int n=i();
             char[][] conn=new char[n][n];
             a=new int[n];
             al=new ArrayList[n];
             vis=new boolean[n];
             vis1=new boolean[n];
             arr=new ArrayList();
             int min=-1;
             for(int i=0;i<n;i++)
             {
              al[i]=new ArrayList();
              a[i]=i();
             }
             for(int i=0;i<n;i++)
             {
              String s=s();
              conn[i]=s.toCharArray();
              for(int j=0;j<conn[i].length;j++)
              {
                if(conn[i][j]=='1'||conn[j][i]=='1')
                {
                  al[i].add(j);
                  al[j].add(i);
                }
              }
             }
             for(int i=0;i<n;i++)
             {
              if(!vis[i]&&!vis1[i])
              {
                dfs(i);
                int j=0;
                Collections.sort(arr);
                for(int m=0;m<n;m++)
                {
                  if(vis[m]&& !vis1[m])
                  {
                      a[m]=(int)arr.get(j);

                      j++;
                      vis1[m]=true;
                  }
                }

              }
              vis=new boolean[n];
              arr=new ArrayList();
             }
             for(int i=0;i<n;i++)out.print(a[i]+" ");

          }
          static void dfs(int u)
          {
              if(vis[u])return;
              vis[u]=true;
              arr.add(a[u]);
              Iterator it=al[u].iterator();
              while(it.hasNext())
              {
                int i=(int)it.next();
                if(!vis[i])
                {
                  dfs(i);
                }

              }
          }
          public void run()
          {
              solve();
              out.close();
          }

          public static void main(String[] args) throws IOException
          {
                new Thread(null, new Sample(), "whatever", 1<<26).start();
          }
         static class Pair implements Comparable<Pair>
         {
            long a;
            int b;
            Pair(){}
            Pair(long a,int b)
            {
                       this.a=a;
                       this.b=b;
            }

            public int compareTo(Pair x)
           {
                return Long.compare(x.a,this.a);
           }
        }

    ////////////////////////////////////////////////////// Merge Sort ////////////////////////////////////////////////////////////////////////

   static class Merge
   {

        public static void sort(long inputArr[])
        {
            int length = inputArr.length;
            doMergeSort(inputArr,0, length - 1);
        }

        private static void doMergeSort(long[] arr,int lowerIndex, int higherIndex)
        {
            if (lowerIndex < higherIndex) {
                int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
                doMergeSort(arr,lowerIndex, middle);
                doMergeSort(arr,middle + 1, higherIndex);
                mergeParts(arr,lowerIndex, middle, higherIndex);
            }
        }

        private static void mergeParts(long[]array,int lowerIndex, int middle, int higherIndex)
        {
            long[] temp=new long[higherIndex-lowerIndex+1];
            for (int i = lowerIndex; i <= higherIndex; i++)
            {
                temp[i-lowerIndex] = array[i];
            }
            int i = lowerIndex;
            int j = middle + 1;
            int k = lowerIndex;
            while (i <= middle && j <= higherIndex)
            {
                if (temp[i-lowerIndex] < temp[j-lowerIndex])
                {
                    array[k] = temp[i-lowerIndex];
                    i++;
                } else {
                    array[k] = temp[j-lowerIndex];
                    j++;
                }
                k++;
            }
            while (i <= middle)
            {
                array[k] = temp[i-lowerIndex];
                k++;
                i++;
            }
            while(j<=higherIndex)
            {
                array[k]=temp[j-lowerIndex];
                k++;
                j++;
            }
        }

    }

    /////////////////////////////////////////////////////////// Methods ////////////////////////////////////////////////////////////////////////

     static boolean isPal(String s)
     {
        for(int i=0, j=s.length()-1;i<=j;i++,j--)
        {
                if(s.charAt(i)!=s.charAt(j)) return false;
        }
        return true;
     }
     static String rev(String s)
     {
                StringBuilder sb=new StringBuilder(s);
                sb.reverse();
                return sb.toString();
     }
     static long gcd(long a,long b){return (a==0)?b:gcd(b%a,a);}
     static long gcdExtended(long a,long b,long[] x)
     {

        if(a==0){
            x[0]=0;
            x[1]=1;
            return b;
        }
        long[] y=new long[2];
        long gcd=gcdExtended(b%a, a, y);

        x[0]=y[1]-(b/a)*y[0];
        x[1]=y[0];

        return gcd;
    }

     boolean findSum(int set[], int n, long sum)
    {
      if (sum == 0)
         return true;
      if (n == 0 && sum != 0)
         return false;
      if (set[n-1] > sum)
         return findSum(set, n-1, sum);
      return findSum(set, n-1, sum) ||findSum(set, n-1, sum-set[n-1]);
    }

      public static long modPow(long base, long exp, long mod)
     {
        base = base % mod;
        long result = 1;
        while (exp > 0)
       {
            if (exp % 2 == 1)
            {
                result = (result * base) % mod;
            }
            base = (base * base) % mod;
            exp = exp >> 1;
       }
        return result;
    }

    static long[] fac;
    static long[] inv;
    static long mod=(long)1e9+7;
    public static void cal()
    {
        fac = new long[1000005];
        inv = new long[1000005];
        fac[0] = 1;
        inv[0] = 1;
        for (int i = 1; i <= 1000000; i++)
        {
            fac[i] = (fac[i - 1] * i) % mod;
            inv[i] = (inv[i - 1] * modPow(i, mod - 2, mod)) % mod;
        }
    }

    public static long ncr(int n, int r)
    {
        return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod;
    }

////////////////////////////////////////// Input Reader ////////////////////////////////////////////////////////////////////////////////////////////////////

    static InputReader sc = new InputReader(System.in);
    static PrintWriter out= new PrintWriter(System.out);

    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;
          }

          public long[] nextLongArray(int n)
          {
                  long a[] = new long[n];
                  for (int i = 0; i < n; i++)
                  {
                          a[i] = nextLong();
                  }
                  return a;
          }

          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 int i()
    {
        return sc.nextInt();
    }
    static long l(){
        return sc.nextLong();
    }
    static int[] iarr(int n)
    {
        return sc.nextIntArray(n);
    }
    static long[] larr(int n)
    {
        return sc.nextLongArray(n);
    }
    static String s(){
        return sc.nextLine();
    }

 }