                                                                                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/2);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
    {

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

 }


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 *
 * @author fz
 */
public class CF_gbB {
    Scanner in = new Scanner(System.in);

    private CF_gbB() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        in.nextLine();
        String[] mat = new String[n];
        for(int i = 0; i < n; i++)
            mat[i] = in.nextLine();

        while(true){
            boolean b = false;
            for(int i = 0; i < n; i++)
                for(int j = i + 1; j < n; j++){
                    if(arr[i] > arr[j] && mat[i].charAt(j) == '1'){
                        int tp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tp;
                        b = true;
                    }
                }
            if(b == false)
                break;
        }

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

    }

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] mat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CF_gbB();
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 *
 * @author fz
 */
public class CF_gbB {
    Scanner in = new Scanner(System.in);

    private CF_gbB() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        in.nextLine();
        String[] mat = new String[n];
        for(int i = 0; i < n; i++)
            mat[i] = in.nextLine();

        for(int i = 0; i < n; i++){
            int idx = i;
            for(int j = i + 1; j < n; j++)
                if(arr[j] < arr[idx])
                    idx = j;
            int cand = idx;
            for(int j = arr[idx] - 1; j < idx; j++){
                if(mat[idx].charAt(j) == '1'){
                    cand = j;
                    break;
                }
            }

            if(arr[idx] < arr[cand]){
                int tp = arr[idx];
                arr[idx] = arr[cand];
                arr[cand] = tp;
            }
            //System.out.print(idx + " " + cand);
            //System.out.println(" " + Arrays.toString(arr));

        }

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

    }

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] mat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CF_gbB();
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 *
 * @author FZ
 */
public class GB_B {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
            arr[i] = sc.nextInt();
        //sc.next();
        String[] s = new String[n];
        for(int i = 0; i < n; i++)
            s[i] = sc.next();

        while(true){
            boolean b = false;
            for(int i = 0; i < n; i++)
                for(int j = i + 1; j < n; j++){
                    if(s[i].charAt(j) == '1' && arr[i] > arr[j]){
                        int tp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tp;
                        b = true;
                    }
                }
            if(b == false)
                break;
        }
        for(int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");

    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 *
 * @author fz
 */
public class CF_gbB {
    Scanner in = new Scanner(System.in);

    private CF_gbB() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        in.nextLine();
        String[] mat = new String[n];
        for(int i = 0; i < n; i++)
            mat[i] = in.nextLine();

        for(int i = 0; i < n; i++){
            int idx = i;
            for(int j = i + 1; j < n; j++)
                if(arr[j] < arr[idx])
                    idx = j;
            int cand = idx;
            for(int j = arr[idx] - 1; j < idx; j++){
                if(mat[idx].charAt(j) == '1' && arr[idx] < arr[j]){
                    cand = j;
                    break;
                }
            }

            //if(arr[idx] < arr[cand]){
                int tp = arr[idx];
                arr[idx] = arr[cand];
                arr[cand] = tp;
            //}
            //System.out.print(idx + " " + cand);
            //System.out.println(" " + Arrays.toString(arr));

        }

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

    }

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] mat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CF_gbB();
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 *
 * @author FZ
 */
public class GB_B {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
            arr[i] = sc.nextInt();
        String[] s = new String[n];
        for(int i = 0; i < n; i++)
            s[i] = sc.nextLine();
        while(true){
            boolean b = false;
            for(int i = 0; i < n; i++)
                for(int j = i + 1; j < n; j++){
                    if(s[i].charAt(j) == '1' && arr[i] > arr[j]){
                        int tp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tp;
                        b = true;
                    }
                }
            if(b == false)
                break;
        }
        for(int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");

    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 *
 * @author fz
 */
public class CF_gbB {
    Scanner in = new Scanner(System.in);

    private CF_gbB() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        in.nextLine();
        String[] mat = new String[n];
        for(int i = 0; i < n; i++)
            mat[i] = in.nextLine();

        List<List<Integer>> sets = new ArrayList<>();
        boolean[] visited = new boolean[n];
        for(int i = 0; i < n; i++){
            if(visited[i] == false){
                List<Integer> ls = new ArrayList<>();
                Stack<Integer> st = new Stack<>();
                st.push(i);
                while(st.isEmpty() == false){
                    int top = st.pop();

                    if(visited[top] == false){
                        visited[top] = true;
                        ls.add(top);
                        for(int j = 0; j < n; j++){
                            if(mat[top].charAt(j) == '1'){
                                st.push(j);
                            }
                        }
                    }
                }
                sets.add(new ArrayList<>(ls));
            }
        }
        for(List<Integer> ls : sets){
            Collections.sort(ls);
            int sz = ls.size();
            for(int i = 0; i < sz; i++)
                for(int j = i + 1; j < sz; j++){
                    if(arr[ls.get(i)] > arr[ls.get(j)]){
                        int tp = arr[ls.get(i)];
                        arr[ls.get(i)] = arr[ls.get(j)];
                        arr[ls.get(j)] = tp;
                    }
                }
        }

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

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] mat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CF_gbB();
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 *
 * @author fz
 */
public class CF_gbB {
    Scanner in = new Scanner(System.in);

    private CF_gbB() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        in.nextLine();
        String[] mat = new String[n];
        for(int i = 0; i < n; i++)
            mat[i] = in.nextLine();

        while(true){
            boolean change = false;
            for(int i = 0; i < n; i++)
                for(int j = i + 1; j < n; j++){
                    if(arr[i] > arr[j] && mat[i].charAt(j) == '1'){
                        int tp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tp;
                        change = true;
                        break;
                    }
                }
            if(change == false)
                break;
        }

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

    }

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] mat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CF_gbB();
    }
}


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

public class Solution {

	private static boolean[] used;
	private static boolean[] visited;
	private static Map<Integer, List<Integer>> graph;
	private static Map<Integer, Set<Integer>> availablePositions;

	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(reader.readLine());
		int[] data = new int[n];
		int[] position = new int[n + 1];
		used = new boolean[n];
		visited = new boolean[n];
		graph = new TreeMap<>();
		availablePositions = new TreeMap<>();

		StringTokenizer st = new StringTokenizer(reader.readLine());
		for (int i = 0; i < n; ++i) {
			data[i] = Integer.parseInt(st.nextToken());
			position[data[i]] = i;
			used[i] = false;

			graph.put(i, new LinkedList<>());
			availablePositions.put(i + 1, new TreeSet<>());
		}

		for (int i = 0; i < n; ++i) {
			String matrixRow = reader.readLine();
			for (int j = 0; j < n; ++j) {
				if (matrixRow.charAt(j) == '1') {
					graph.get(i).add(j);

					if (i != j)
						graph.get(j).add(i);
				}
			}
		}

		reader.close();

		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j < n; ++j)
				visited[j] = false;

			setAvailablePositions(availablePositions.get(i), position[i]);
		}

		for (int i = 1; i <= n; ++i) {
			for (Integer pos: availablePositions.get(i)) {
				if (used[pos])
					continue;

				used[pos] = true;
				data[pos] = i;
				break;
			}
		}

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

	private static void setAvailablePositions(Set<Integer> set, int pos) {
		visited[pos] = true;
		set.add(pos);

		for (Integer nextPos: graph.get(pos)) {
			if (visited[nextPos])
				continue;

			setAvailablePositions(set, nextPos);
		}
	}
}


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

public class Solution {

	private static boolean[] used;
	private static boolean[] visited;
	private static Map<Integer, List<Integer>> graph;
	private static Map<Integer, Set<Integer>> availablePositions;

	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(reader.readLine());
		int[] data = new int[n];
		int[] position = new int[n + 1];
		used = new boolean[n];
		visited = new boolean[n];
		graph = new TreeMap<>();
		availablePositions = new TreeMap<>();

		StringTokenizer st = new StringTokenizer(reader.readLine());
		for (int i = 0; i < n; ++i) {
			data[i] = Integer.parseInt(st.nextToken());
			position[data[i]] = i;
			used[i] = false;

			graph.put(i, new LinkedList<>());
			availablePositions.put(i + 1, new TreeSet<>());
		}

		for (int i = 0; i < (n + 1) / 2; ++i) {
			String matrixRow = reader.readLine();
			for (int j = i; j < n; ++j) {
				if (matrixRow.charAt(j) == '1') {
					graph.get(i).add(j);

					if (i != j)
						graph.get(j).add(i);
				}
			}
		}

		reader.close();

		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j < n; ++j)
				visited[j] = false;

			setAvailablePositions(availablePositions.get(i), position[i]);
		}

		for (int i = 1; i <= n; ++i) {
			for (Integer pos: availablePositions.get(i)) {
				if (used[pos])
					continue;

				used[pos] = true;
				data[pos] = i;
				break;
			}
		}

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

	private static void setAvailablePositions(Set<Integer> set, int pos) {
		visited[pos] = true;
		set.add(pos);

		for (Integer nextPos: graph.get(pos)) {
			if (visited[nextPos])
				continue;

			setAvailablePositions(set, nextPos);
		}
	}
}


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

public class Solution {

	private static boolean[] used;
	private static boolean[] visited;
	private static Map<Integer, List<Integer>> graph;
	private static Map<Integer, Set<Integer>> availablePositions;

	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(reader.readLine());
		int[] data = new int[n];
		int[] position = new int[n + 1];
		used = new boolean[n];
		visited = new boolean[n];
		graph = new TreeMap<>();
		availablePositions = new TreeMap<>();

		StringTokenizer st = new StringTokenizer(reader.readLine());
		for (int i = 0; i < n; ++i) {
			data[i] = Integer.parseInt(st.nextToken());
			position[data[i]] = i;
			used[i] = false;

			graph.put(i, new LinkedList<>());
			availablePositions.put(i + 1, new TreeSet<>());
		}

		for (int i = 0; i < (n + 1) / 2; ++i) {
			String matrixRow = reader.readLine();
			for (int j = i; j < n; ++j) {
				if (matrixRow.charAt(j) == '1') {
					graph.get(i).add(j);
					graph.get(j).add(i);
				}
			}
		}

		reader.close();

		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j < n; ++j)
				visited[j] = false;

			setAvailablePositions(availablePositions.get(i), position[i]);
		}

		for (int i = 1; i <= n; ++i) {
			for (Integer pos: availablePositions.get(i)) {
				if (used[pos])
					continue;

				used[pos] = true;
				data[pos] = i;
				break;
			}
		}

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

	private static void setAvailablePositions(Set<Integer> set, int pos) {
		visited[pos] = true;
		set.add(pos);

		for (Integer nextPos: graph.get(pos)) {
			if (visited[nextPos])
				continue;

			setAvailablePositions(set, nextPos);
		}
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.PriorityQueue;

/**
 * @author Mubtasim Shahriar
 */

public class NewYearPer {

	public static void main(String[] args) {

		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader sc = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new Solver();
//		int t = sc.nextInt();
		int t = 1;
		while(t--!=0) {
			solver.solve(sc, out);
		}
		out.close();

	}

	static ArrayList<Integer>[] gp;
	static boolean[] vis;
	static int[][] mat;
	static int[] perm;
	static PriorityQueue<Integer> cmp;
	static ArrayList<Integer> idxs;

	static class Solver {
		public void solve(InputReader sc, PrintWriter out) {
			int n = sc.nextInt();
			gp = new ArrayList[n];
			for(int i = 0; i < n; i++) gp[i] = new ArrayList();
			vis = new boolean[n];
			mat = new int[n][n];
			perm = sc.nextIntArray(n);
			for(int i = 0; i < n; i++) {
				String s = sc.next();
				for(int j = 0; j < n; j++) {
					int now = Character.getNumericValue(s.charAt(j));
					mat[i][j] = now;
					if(now==1) {
						gp[i].add(j);
					}
				}
			}
			for(int i = 0; i < n; i++) {
				if(!vis[i]) {
					cmp = new PriorityQueue();
					idxs = new ArrayList();
					dfs(i);
					Collections.sort(idxs);
					for(int j : idxs) cmp.add(perm[j]);
					for(int j : idxs) perm[j] = cmp.poll();
				}
			}
			for(int i : perm) out.print(i + " ");
			out.println();
		}

		private void dfs(int i) {
			vis[i] = true;
			idxs.add(i);
			for(int v : gp[i]) {
				if(!vis[v]) dfs(v);
			}
		}
	}

	static class InputReader {
		private boolean finished = false;

		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;

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

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

		public int peek() {
			if (numChars == -1) {
				return -1;
			}
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					return -1;
				}
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar];
		}

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

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

		public String nextString() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				if (Character.isValidCodePoint(c)) {
					res.appendCodePoint(c);
				}
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			if (filter != null) {
				return filter.isSpaceChar(c);
			}
			return isWhitespace(c);
		}

		public static boolean isWhitespace(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private String readLine0() {
			StringBuilder buf = new StringBuilder();
			int c = read();
			while (c != '\n' && c != -1) {
				if (c != '\r') {
					buf.appendCodePoint(c);
				}
				c = read();
			}
			return buf.toString();
		}

		public String readLine() {
			String s = readLine0();
			while (s.trim().length() == 0) {
				s = readLine0();
			}
			return s;
		}

		public String readLine(boolean ignoreEmptyLines) {
			if (ignoreEmptyLines) {
				return readLine();
			} else {
				return readLine0();
			}
		}

		public BigInteger readBigInteger() {
			try {
				return new BigInteger(nextString());
			} catch (NumberFormatException e) {
				throw new InputMismatchException();
			}
		}

		public char nextCharacter() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			return (char) c;
		}

		public double nextDouble() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.') {
				if (c == 'e' || c == 'E') {
					return res * Math.pow(10, nextInt());
				}
				if (c < '0' || c > '9') {
					throw new InputMismatchException();
				}
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.') {
				c = read();
				double m = 1;
				while (!isSpaceChar(c)) {
					if (c == 'e' || c == 'E') {
						return res * Math.pow(10, nextInt());
					}
					if (c < '0' || c > '9') {
						throw new InputMismatchException();
					}
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public boolean isExhausted() {
			int value;
			while (isSpaceChar(value = peek()) && value != -1) {
				read();
			}
			return value == -1;
		}

		public String next() {
			return nextString();
		}

		public SpaceCharFilter getFilter() {
			return filter;
		}

		public void setFilter(SpaceCharFilter filter) {
			this.filter = filter;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
		public int[] nextIntArray(int n){
			int[] array=new int[n];
			for(int i=0;i<n;++i)array[i]=nextInt();
			return array;
		}
		public int[] nextSortedIntArray(int n){
			int array[]=nextIntArray(n);
			Arrays.sort(array);
			return array;
		}
		public int[] nextSumIntArray(int n){
			int[] array=new int[n];
			array[0]=nextInt();
			for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
			return array;
		}
		public long[] nextLongArray(int n){
			long[] array=new long[n];
			for(int i=0;i<n;++i)array[i]=nextLong();
			return array;
		}
		public long[] nextSumLongArray(int n){
			long[] array=new long[n];
			array[0]=nextInt();
			for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
			return array;
		}
		public long[] nextSortedLongArray(int n){
			long array[]=nextLongArray(n);
			Arrays.sort(array);
			return array;
		}
	}

}


import java.util.*;
import java.io.*;
import java.math.*;
public class Main9
{
	static class Pair
	{
		int x;
		int y;
		Pair(int x ,int y)
		{
			this.x=x;
			this.y=y;
		}
	}
	static int mod=1000000007;
	public static int[] sort(int[] a)
	{
		int n=a.length;
		ArrayList<Integer> ar=new ArrayList<>();
		for(int i=0;i<a.length;i++)
		{
			ar.add(a[i]);
		}
		Collections.sort(ar);
		for(int i=0;i<n;i++)
		{
			a[i]=ar.get(i);
		}
		return a;
	}
	public static long pow(long a, long b)
    {
        long result=1;
        while(b>0)
        {
            if (b % 2 != 0)
            {
                result=(result*a)%mod;
                b--;
            }
            a=(a*a)%mod;
            b /= 2;
        }
        return result;
    }

    public static long gcd(long a, long b)
    {
        if (a == 0)
            return b;
        return gcd(b%a, a);
    }
	static ArrayList<ArrayList<Integer>> graph;
	static public void main(String args[])throws IOException
	{
		int tt=1;
		StringBuilder sb=new StringBuilder();
		for(int ttt=1;ttt<=tt;ttt++)
		{
			int n=i();
			int[] a=new int[n];
			for(int i=0;i<n;i++)
			{
				a[i]=i();
			}
			char[][] ch=new char[n][n];
			for(int i=0;i<n;i++)
			{
				String s=s();
				for(int j=0;j<n;j++)
				{
					ch[i][j]=s.charAt(j);
				}
			}
			boolean[] visited=new boolean[n];
			int[] ans=new int[n];
			for(int i=0;i<n;i++)
			{
				if(visited[i]==false)
				{
					Queue<Integer> queue=new LinkedList<>();
					queue.add(i);
					visited[i]=true;
					ArrayList<Integer> ar=new ArrayList<>();
					ArrayList<Integer> index=new ArrayList<>();
					while(queue.isEmpty()==false)
					{
						int num=queue.poll();
						ar.add(a[num]);
						index.add(num);
						for(int j=0;j<n;j++)
						{
							if(num!=j && visited[j]==false && ch[num][j]=='1')
							{
								queue.add(j);
								visited[j]=true;
							}
						}
					}
					Collections.sort(ar);
					Collections.sort(index);
					for(int  j=0;j<ar.size();j++)
					{
						ans[index.get(j)]=ar.get(j);
					}
				}
			}
			for(int i=0;i<n;i++)
			{
				sb.append(ans[i]+" ");
			}
			sb.append("\n");

		}
		System.out.print(sb.toString());
	}
			/**/
	static InputReader in=new InputReader(System.in);
	static OutputWriter out=new OutputWriter(System.out);
	public static long l()
	{
		String s=in.String();
		return Long.parseLong(s);
	}
	public static void pln(String value)
	{
		System.out.println(value);
	}
	public static int i()
	{
		return in.Int();
	}
	public static String s()
	{
		return in.String();
	}
}
	class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;

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

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

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

		public String String() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(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;
		}

		public String next() {
			return String();
		}

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

	class OutputWriter {
		private final PrintWriter writer;

		public OutputWriter(OutputStream outputStream) {
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
		}

		public OutputWriter(Writer writer) {
			this.writer = new PrintWriter(writer);
		}

		public void print(Object...objects) {
			for (int i = 0; i < objects.length; i++) {
				if (i != 0)
					writer.print(' ');
				writer.print(objects[i]);
			}
		}

		public void printLine(Object...objects) {
			print(objects);
			writer.println();
		}

		public void close() {
			writer.close();
		}

		public void flush() {
			writer.flush();
		}

		}

		class IOUtils {

		public static int[] readIntArray(InputReader in, int size) {
			int[] array = new int[size];
			for (int i = 0; i < size; i++)
				array[i] = in.Int();
			return array;
		}

	}


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

public class Main {
    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    static int[] pos;
    static int[] p;
    static int n;
    static boolean[][] m;

    static void solve() throws Exception {
        n = nextInt();
        pos = new int[n];
        int[] a = new int[n];
        for (int i = 0; i < n; ++i) {
            a[i] = nextInt() - 1;
            pos[a[i]] = i;
        }
        m = new boolean[n][n];
        for (int i = 0; i < n; ++i) {
            String s = next();
            for (int j = 0; j < n; ++j) {
                if (s.charAt(j) == '1') {
                    m[a[i]][a[j]] = true;
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            p = new int[n];
            Arrays.fill(p, -1);
            p[i] = i;
            dfs(i);
            int x = i;
            for (int j = 0; j < n; ++j) {
                if (p[j] != -1 && (pos[j] < pos[x])) {
                    x = j;
                }
            }
            if (x != i) {
                ArrayList<Integer> way = new ArrayList<Integer>();
                while (p[x] != x) {
                    way.add(x);
                    x = p[x];
                }
                for (int j = way.size() - 1; j >= 0; --j) {
                    int tmp = pos[way.get(j)];
                    pos[way.get(j)] = pos[i];
                    pos[i] = tmp;
                }
            }
            for (int j = 0; j < n; ++j) {
                m[j][i] = false;
                m[i][j] = false;
            }
        }
        int[] res = new int[n];
        for (int i = 0; i < n; ++i) {
            res[pos[i]] = i;
        }
        for (int i = 0; i < n; ++i) {
            out.print((res[i] + 1) + " ");
        }
        out.println();
    }

    static void dfs(int x) {
        for (int j = 0; j < n; ++j) {
            if (m[x][j] == true && p[j] == -1) {
                p[j] = x;
                dfs(j);
            }
        }
    }

    static long sqr(int x) {
        return (long)x * x;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    static long lcm(int a, int b) {
        return (long)a / gcd(a, b) * b;
    }

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

    static BigInteger nextBigInteger() throws IOException {
        return new BigInteger(next());
    }

    static String next() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    static String nextLine() throws IOException {
        tok = new StringTokenizer("");
        return in.readLine();
    }

    static boolean hasNext() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return false;
            }
            tok = new StringTokenizer(s);
        }
        return true;
    }

    public static void main(String args[]) {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
            //in = new BufferedReader(new FileReader("input.in"));
            //out = new PrintWriter(new FileWriter("output.out"));
            solve();
            in.close();
            out.close();
        } catch (Throwable e) {
            e.printStackTrace();
            java.lang.System.exit(1);
        }
    }
}


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

public class Main {
    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    static int n;
    static char[][] s;
    static boolean[] used1;
    static int[] pos;

    static void solve() throws Exception {
        n = nextInt();
        pos = new int[n];
        for (int i = 0; i < n; ++i) {
            pos[nextInt() - 1] = i;
        }
        s = new char[n][n];
        for (int i = 0; i < n; ++i) {
            s[i] = next().toCharArray();
        }
        boolean[] used2 = new boolean[n];
        int[] res = new int[n];
        for (int i = 0; i < n; ++i) {
            used1 = new boolean[n];
            dfs(i);
            for (int j = 0; j < n; ++j) {
                if (used1[j] && !used2[j]) {
                    used2[j] = true;
                    res[j] = i + 1;
                    break;
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            out.print(res[i] + " ");
        }
        out.println();
    }

    static void dfs(int x) {
        used1[pos[x]] = true;
        for (int j = 0; j < n; ++j) {
            if (s[pos[x]][pos[j]] == '1' && !used1[pos[j]]) {
                dfs(j);
            }
        }
    }

    static long sqr(int x) {
        return (long)x * x;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    static long lcm(int a, int b) {
        return (long)a / gcd(a, b) * b;
    }

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

    static BigInteger nextBigInteger() throws IOException {
        return new BigInteger(next());
    }

    static String next() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    static String nextLine() throws IOException {
        tok = new StringTokenizer("");
        return in.readLine();
    }

    static boolean hasNext() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return false;
            }
            tok = new StringTokenizer(s);
        }
        return true;
    }

    public static void main(String args[]) {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
            //in = new BufferedReader(new FileReader("input.in"));
            //out = new PrintWriter(new FileWriter("output.out"));
            solve();
            in.close();
            out.close();
        } catch (Throwable e) {
            e.printStackTrace();
            java.lang.System.exit(1);
        }
    }
}


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

public class Main {
    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    static int[] pos;
    static int[] p;
    static char[][] s;
    static int n;

    static void solve() throws Exception {
        n = nextInt();
        pos = new int[n];
        for (int i = 0; i < n; ++i) {
            pos[nextInt() - 1] = i;
        }
        s = new char[n][n];
        for (int i = 0; i < n; ++i) {
            s[i] = next().toCharArray();
        }
        for (int i = 0; i < n; ++i) {
            p = new int[n];
            Arrays.fill(p, -1);
            p[i] = i;
            dfs(i);
            int x = i;
            for (int j = 0; j < n; ++j) {
                if (p[j] != -1 && (pos[j] < pos[x])) {
                    x = j;
                }
            }
            if (x != i) {
                ArrayList<Integer> way = new ArrayList<Integer>();
                while (p[x] != x) {
                    way.add(x);
                    x = p[x];
                }
                for (int j = way.size() - 1; j >= 0; --j) {
                    int tmp = pos[way.get(j)];
                    pos[way.get(j)] = pos[i];
                    pos[i] = tmp;
                }
            }
            for (int j = 0; j < n; ++j) {
                s[j][i] = '0';
                s[i][j] = '0';
            }
        }
        int[] res = new int[n];
        for (int i = 0; i < n; ++i) {
            res[pos[i]] = i;
        }
        for (int i = 0; i < n; ++i) {
            out.print((res[i] + 1) + " ");
        }
        out.println();
    }

    static void dfs(int x) {
        for (int j = 0; j < n; ++j) {
            if (s[pos[x]][pos[j]] == '1' && p[j] == -1) {
                p[j] = x;
                dfs(j);
            }
        }
    }

    static long sqr(int x) {
        return (long)x * x;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    static long lcm(int a, int b) {
        return (long)a / gcd(a, b) * b;
    }

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

    static BigInteger nextBigInteger() throws IOException {
        return new BigInteger(next());
    }

    static String next() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    static String nextLine() throws IOException {
        tok = new StringTokenizer("");
        return in.readLine();
    }

    static boolean hasNext() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return false;
            }
            tok = new StringTokenizer(s);
        }
        return true;
    }

    public static void main(String args[]) {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
            //in = new BufferedReader(new FileReader("input.in"));
            //out = new PrintWriter(new FileWriter("output.out"));
            solve();
            in.close();
            out.close();
        } catch (Throwable e) {
            e.printStackTrace();
            java.lang.System.exit(1);
        }
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Codeforces;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class c500B {
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        int n = inp.nextInt();
        int[] p = new int[305];
        int[][] a = new int[305][305];

        for (int i = 1; i <= n; i++) {
            p[i] = inp.nextInt();
        }
        String s;
        for (int i = 1; i <= n; i++) {
            s = inp.next();
            for (int j = 0; j < n; j++) {
                a[i][j+1] = (int) s.charAt(j) - 48;
            }
        }
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (a[i][k] == 1 && a[k][j] == 1 && i != j) {
                        a[i][j] = 1;
                    }
                }
            }
        }
        int pos=1;
        int d,min,minpos;
        int tmp;
        while (pos <= n)
        {
            min = p[pos];
            minpos = pos;
            for (int i=pos+1;i<=n;i++)
                if (p[i] < p[pos] && a[i][pos] == 1)
                {
                    if (p[i] < min)
                    {
                        min = p[i];
                        minpos = i;
                    }
                }
            if (minpos != pos && p[minpos] < p[pos])
            {
                tmp = p[minpos];
                p[minpos]  = p[pos];
                p[pos] = tmp;
            }
            pos++;
        }
        for (int i=1;i<n;i++)
            System.out.print(p[i] + " ");
        System.out.print(p[n]);
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package Codeforces;

/**
 *
 * @author Administrator
 */
import java.util.Scanner;
public class c500B {
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        int n = inp.nextInt();
        int[] p = new int[305];
        int[][] a = new int[305][305];

        for (int i = 1; i <= n; i++) {
            p[i] = inp.nextInt();
        }
        String s;
        for (int i = 1; i <= n; i++) {
            s = inp.next();
            for (int j = 0; j < n; j++) {
                a[i][j+1] = (int) s.charAt(j) - 48;
            }
        }
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (a[i][k] == 1 && a[k][j] == 1 && i != j) {
                        a[i][j] = 1;
                    }
                }
            }
        }
        int pos=1;
        int d,min,minpos;
        int tmp;
        while (pos <= n)
        {
            min = p[pos];
            minpos = pos;
            for (int i=pos+1;i<=n;i++)
                if (p[i] < p[pos] && a[i][pos] == 1)
                {
                    if (p[i] < min)
                    {
                        min = p[i];
                        minpos = i;
                    }
                }
            if (minpos != pos && p[minpos] < p[pos])
            {
                tmp = p[minpos];
                p[minpos]  = p[pos];
                p[pos] = tmp;
            }
            pos++;
        }
        for (int i=1;i<n;i++)
            System.out.print(p[i] + " ");
        System.out.print(p[n]);
    }
}


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

public class B500 {
    static int n;
    static int[] arr;
    static char[][] matrix;
    static int[] parent;
    static boolean[] visited;
    public static void main(String[] args) throws IOException {
        PrintWriter w = new PrintWriter(System.out);
        InputReader in = new InputReader(System.in);
        n = in.nextInt();
        arr = new int[n+1];
        parent = new int[n+1];
        for (int i=1; i<=n; i++) {
            arr[i] = in.nextInt();
            parent[i] = i;
        }
        matrix = new char[n][n];
        for (int i=0; i<n; i++) {
            String s = in.nextString();
            for (int j=0; j<n; j++) {
                matrix[i][j] = s.charAt(j);
                if (matrix[i][j] == '1') {
                    union(i+1, j+1);
                }
            }
        }
        visited = new boolean[n+1];
        for (int i=1; i<=n; i++) {
            if (!visited[i]) {
                List<Integer> list1 = new ArrayList<>();
                list1.add(i);
                int value = parent[i];
                visited[i] = true;
                for (int j=1; j<=n; j++) {
                    if (!visited[j] && parent[j] == value) {
                        visited[j] = true;
                        list1.add(j);
                    }
                }
                List<Integer> list2 = new ArrayList<>();
                Collections.sort(list1);
                for (int j : list1) {
                    list2.add(arr[j]);
                }
                Collections.sort(list2);
                for (int j=0; j<list1.size(); j++) {
                    arr[list1.get(j)] = list2.get(j);
                }
            }
        }
        for (int i=1; i<=n; i++) {
            w.print(arr[i] + " ");
        }
        w.println();
        w.close();
    }
    static void union(int a, int b) {
        a = find(a);
        b = find(b);
        if (a != b) {
            parent[b] = a;
        }
    }
    static int find(int a) {
        if (parent[a] == a) return a;
        return parent[a] = find(parent[a]);
    }
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

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

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

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

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

        public String nextString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        }

    }

}


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

public class B500 {
    static int n;
    static int[] arr;
    static char[][] matrix;
    static List<Integer>[] graph;
    static boolean[] visited;
    static List<Integer> list1, list2;
    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);
        n = in.nextInt();
        arr = new int[n+1];
        matrix = new char[n][n];
        graph = new ArrayList[n+1];
        Arrays.setAll(graph, i -> new ArrayList<>());
        for (int i=1; i<=n; i++) {
            arr[i] = in.nextInt();
        }
        for (int i=0; i<n; i++) {
            String s = in.nextString();
            for (int j=0; j<n; j++) {
                matrix[i][j] = s.charAt(j);
                if (matrix[i][j] == '1') {
                    graph[i+1].add(j+1);
                    graph[j+1].add(i+1);
                }
            }
        }
        visited = new boolean[n+1];
        for (int i=1; i<=n; i++) {
            if (!visited[i]) {
                list1 = new ArrayList<>();
                dfs(i);
                list2 = new ArrayList<>();
                for (int j : list1) {
                    list2.add(arr[j]);
                }
                Collections.sort(list2);
                Collections.sort(list1);
                for (int j=0; j<list1.size(); j++) {
                    arr[list1.get(j)] = list2.get(j);
                }
            }
        }
        for (int i=1; i<=n; i++) {
            w.print(arr[i] + " ");
        }
        w.println();
        w.close();
    }
    static void dfs(int u) {
        list1.add(u);
        visited[u] = true;
        for (int v : graph[u]) {
            if (!visited[v]) {
                dfs(v);
            }
        }
    }
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

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

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

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

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

        public String nextString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        }

    }

}


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

public class B500 {
    static int n;
    static int[] arr;
    static char[][] matrix;
    static int[] parent;
    static boolean[] visited;
    public static void main(String[] args) throws IOException {
        PrintWriter w = new PrintWriter(System.out);
        InputReader in = new InputReader(System.in);
        n = in.nextInt();
        arr = new int[n+1];
        parent = new int[n+1];
        for (int i=1; i<=n; i++) {
            arr[i] = in.nextInt();
            parent[i] = i;
        }
        matrix = new char[n][n];
        for (int i=0; i<n; i++) {
            String s = in.nextString();
            for (int j=0; j<n; j++) {
                matrix[i][j] = s.charAt(j);
                if (matrix[i][j] == '1') {
                    union(i+1, j+1);
                }
            }
        }
        visited = new boolean[n+1];
        for (int i=1; i<=n; i++) {
            //if (!visited[i]) {
                List<Integer> list1 = new ArrayList<>();
                //list1.add(i);
                int value = parent[i];
                //visited[i] = true;
                for (int j=1; j<=n; j++) {
                    if (parent[j] == value) {
                        //visited[j] = true;
                        list1.add(j);
                    }
                }
                List<Integer> list2 = new ArrayList<>();
                Collections.sort(list1);
                for (int j : list1) {
                    list2.add(arr[j]);
                }
                Collections.sort(list2);
                for (int j=0; j<list1.size(); j++) {
                    arr[list1.get(j)] = list2.get(j);
                }
            //}
        }
        for (int i=1; i<=n; i++) {
            w.print(arr[i] + " ");
        }
        w.println();
        w.close();
    }
    static void union(int a, int b) {
        a = find(a);
        b = find(b);
        if (a != b) {
            parent[b] = a;
        }
    }
    static int find(int a) {
        if (parent[a] == a) return a;
        return parent[a] = find(parent[a]);
    }
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

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

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

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

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

        public String nextString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        }

    }

}


import java.util.InputMismatchException;
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 {
    int n;
    int[] p;
    int[] path;
    char[][] a;
    boolean[] used;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        p = new int[n];
        a = new char[n][n];
        for (int i = 0; i < n; ++i)
            p[i] = in.nextInt();
        for (int i = 0; i < n; ++i)
            a[i] = in.next().toCharArray();

        path = new int[n];
        used = new boolean[n];
        for (int i = 0; i < n; ++i) {
            int minID = minValueID(i);
            for (int j = minID; j != i; j = path[j]){
                int tmp = p[j];
                p[j] = p[path[j]];
                p[path[j]] = tmp;
            }
            for (int j = i + 1; j < n; ++j)
                used[j] = false;
        }

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

    int minValueID(int v) {
        used[v] = true;
        path[v] = v;
        int res = v;
        for (int i = 0; i < n; ++i)
            if (!used[i] && a[v][i] == '1') {
                int tmpID = minValueID(i);
                if (p[res] > p[tmpID]) {
                    res = tmpID;
                    path[tmpID] = v;
                }
            }

        return res;
    }
}

class InputReader {
    BufferedReader in;
    StringTokenizer st;

    public InputReader(InputStream stream) {
        in = new BufferedReader(new InputStreamReader(stream));
        eat("");
    }

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

    public String next() {
        while (!st.hasMoreTokens())
            eat(nextLine());
        return st.nextToken();
    }

    public String nextLine() {
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public void eat(String str) {
        if (str == null) throw new InputMismatchException();
        st = new StringTokenizer(str);
    }
}


import java.util.Arrays;
import java.util.InputMismatchException;
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 {
    int n;
    int[] p;
    char[][] a;
    boolean[] used;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        p = new int[n];
        a = new char[n][n];
        for (int i = 0; i < n; ++i)
            p[i] = in.nextInt();
        for (int i = 0; i < n; ++i)
            a[i] = in.next().toCharArray();

        used = new boolean[n];
        int[] res = new int[n];
        for (int i = 0; i < n; ++i) {
            int minID = minValueID(i);
            res[i] = p[minID];
            p[minID] = p[i];
            p[i] = Integer.MAX_VALUE;
            Arrays.fill(used, false);
        }

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

    int minValueID(int v) {
        used[v] = true;
        int res = v;
        for (int i = 0; i < n; ++i)
            if (!used[i] && a[v][i] == '1') {
                int tmpID = minValueID(i);
                if (p[res] > p[tmpID])
                    res = tmpID;
            }

        return res;
    }
}

class InputReader {
    BufferedReader in;
    StringTokenizer st;

    public InputReader(InputStream stream) {
        in = new BufferedReader(new InputStreamReader(stream));
        eat("");
    }

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

    public String next() {
        while (!st.hasMoreTokens())
            eat(nextLine());
        return st.nextToken();
    }

    public String nextLine() {
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public void eat(String str) {
        if (str == null) throw new InputMismatchException();
        st = new StringTokenizer(str);
    }
}


import java.util.InputMismatchException;
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 {
    int n;
    int[] p;
    char[][] a;
    boolean[] used;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        p = new int[n];
        a = new char[n][n];
        for (int i = 0; i < n; ++i)
            p[i] = in.nextInt();
        for (int i = 0; i < n; ++i)
            a[i] = in.next().toCharArray();

        used = new boolean[n];
        for (int i = 0; i < n; ++i) {
            int minID = minValueID(i);
            int tmp = p[minID];
            p[minID] = p[i];
            p[i] = tmp;
            for (int j = i + 1; j < n; ++j)
                used[j] = false;
        }

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

    int minValueID(int v) {
        used[v] = true;
        int res = v;
        for (int i = 0; i < n; ++i)
            if (!used[i] && a[v][i] == '1') {
                int tmpID = minValueID(i);
                if (p[res] > p[tmpID])
                    res = tmpID;
            }

        return res;
    }
}

class InputReader {
    BufferedReader in;
    StringTokenizer st;

    public InputReader(InputStream stream) {
        in = new BufferedReader(new InputStreamReader(stream));
        eat("");
    }

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

    public String next() {
        while (!st.hasMoreTokens())
            eat(nextLine());
        return st.nextToken();
    }

    public String nextLine() {
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public void eat(String str) {
        if (str == null) throw new InputMismatchException();
        st = new StringTokenizer(str);
    }
}


import java.util.InputMismatchException;
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 {
    int n;
    int[] p;
    int[] path;
    char[][] a;
    boolean[] used;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        p = new int[n];
        a = new char[n][n];
        for (int i = 0; i < n; ++i)
            p[i] = in.nextInt();
        for (int i = 0; i < n; ++i)
            a[i] = in.next().toCharArray();

        path = new int[n];
        used = new boolean[n];
        for (int i = 0; i < n; ++i) {
            int minID = minValueID(i);
            for (int j = minID; j != i; j = path[j]){
                int tmp = p[j];
                p[j] = p[path[j]];
                p[path[j]] = tmp;
            }
            for (int j = i + 1; j < n; ++j)
                used[j] = false;
        }

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

    int minValueID(int v) {
        used[v] = true;
        path[v] = v;
        int res = v;
        for (int i = 0; i < n; ++i)
            if (!used[i] && a[v][i] == '1') {
                int tmpID = minValueID(i);
                if (p[res] > p[tmpID])
                    res = tmpID;
                path[i] = v;
            }

        return res;
    }
}

class InputReader {
    BufferedReader in;
    StringTokenizer st;

    public InputReader(InputStream stream) {
        in = new BufferedReader(new InputStreamReader(stream));
        eat("");
    }

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

    public String next() {
        while (!st.hasMoreTokens())
            eat(nextLine());
        return st.nextToken();
    }

    public String nextLine() {
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public void eat(String str) {
        if (str == null) throw new InputMismatchException();
        st = new StringTokenizer(str);
    }
}


import java.util.Arrays;
import java.util.InputMismatchException;
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 {
    int n;
    int[] p;
    char[][] a;
    boolean[] used;
    int cnt;
    int[] values;
    int[] indices;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        p = new int[n];
        a = new char[n][n];
        for (int i = 0; i < n; ++i)
            p[i] = in.nextInt();
        for (int i = 0; i < n; ++i)
            a[i] = in.next().toCharArray();

        values = new int[n];
        indices = new int[n];
        used = new boolean[n];
        for (int i = 0; i < n; ++i) {
            if (used[i]) continue;
            cnt = 0;
            dfs(i);
            Arrays.sort(indices, 0, cnt);
            Arrays.sort(values, 0, cnt);
            for (int j = 0; j < cnt; ++j) p[indices[j]] = values[j];
        }

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

    void dfs(int v) {
        used[v] = true;
        indices[cnt] = v;
        values[cnt++] = p[v];
        for (int i = 0; i < n; ++i)
            if (!used[i] && a[v][i] == '1') dfs(i);
    }
}

class InputReader {
    BufferedReader in;
    StringTokenizer st;

    public InputReader(InputStream stream) {
        in = new BufferedReader(new InputStreamReader(stream));
        eat("");
    }

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

    public String next() {
        while (!st.hasMoreTokens())
            eat(nextLine());
        return st.nextToken();
    }

    public String nextLine() {
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public void eat(String str) {
        if (str == null) throw new InputMismatchException();
        st = new StringTokenizer(str);
    }
}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class Abood2C {

	static String[] ss;
	static int[] arr;
	static ArrayList<Integer> idx, v;
	static boolean [] vis;

	static void dfs(int u){
		vis[u] = true;
		idx.add(u);
		v.add(arr[u]);

		for (int i = 0; i < arr.length; i++) {
			if(!vis[i] && (ss[u].charAt(i) == '1' || ss[i].charAt(u) == '1'))
				dfs(i);
		}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();

		arr = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		ss = new String[n];
		for (int i = 0; i < n; i++) {
			ss[i] = sc.next();
		}

		vis = new boolean[n];

		for (int i = 0; i < n; i++) {
			if(!vis[i]){
				idx = new ArrayList<>();
				v = new ArrayList<>();
				dfs(i);
				Collections.sort(idx);
				Collections.sort(v);
				for (int j = 0; j < v.size(); j++) {
					arr[idx.get(j)] = v.get(j);
				}
			}
		}

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

		out.println();

		out.flush();
		out.close();
	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

		public String nextLine() throws IOException {return br.readLine();}

		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-')
			{
				neg = true;
				start++;
			}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else
				{
					sb.append(x.charAt(i));
					if(dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}

		public boolean ready() throws IOException {return br.ready();}

	}

}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Abood2C {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();

		int[] arr = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		String[] ss = new String[n];
		for (int i = 0; i < n; i++) {
			ss[i] = sc.next();
		}

		while(true){
			boolean f = true;
			for (int i = 0; i < n; i++) {
				String s = ss[i];
				for (int j = 0; j < n; j++) {
					if(i == j || s.charAt(j) == '0')
						continue;

					int[] tmp = new int[n];

					for (int k = 0; k < n; k++) {
						tmp[k] = arr[k];
					}

					int tt = tmp[i];
					tmp[i] = tmp[j];
					tmp[j] = tt;

					for (int k = 0; k < n; k++) {
						if(arr[k] < tmp[k])
							break;

						if(arr[k] > tmp[k]){
							arr = tmp;
							f = false;
							break;
						}
					}
				}

			}

			if(f)
				break;
		}

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

		out.println();

		out.flush();
		out.close();
	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

		public String nextLine() throws IOException {return br.readLine();}

		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-')
			{
				neg = true;
				start++;
			}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else
				{
					sb.append(x.charAt(i));
					if(dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}

		public boolean ready() throws IOException {return br.ready();}

	}

}


import java.util.*;
import java.util.stream.Collectors;
public class NewYearPermutation {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scan.nextInt();
		}
		int[][] adj=new int[n][n];
		boolean[] visited=new boolean[n];
		Arrays.fill(visited,false);
		for(int i=0;i<n;i++) {
			String str=scan.next();
			for(int j=0;j<n;j++) {
				adj[i][j]=Character.getNumericValue(str.charAt(j));
			}
		}
		int[] ans=new int[n];
		for(int i=0;i<n;i++) {
			if(!visited[i]) {
				ArrayList<Integer> al=new ArrayList();
				Queue<Integer> queue=new LinkedList<Integer>();
				queue.add(i);
				visited[i]=true;
				al.add(i);
				while(queue.size()!=0) {
					int val=queue.poll();
					//System.out.print(val+" ");
					visited[val]=true;
					for(int j=0;j<n;j++) {
						if(adj[val][j]==1 && !visited[j]) {
							visited[j]=true;
							al.add(j);
							queue.add(j);
						}
					}
				}
				int len=al.size();
				List<Integer> temp1= al.stream().map(p-> arr[p]).collect(Collectors.toList());
				Collections.sort(temp1);
				Collections.sort(al);
				for(int j=0;j<len;j++) {
					ans[al.get(j)]=temp1.get(j);
				}

			}

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