#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
void dfs(long long int e)
{
    visited[e]=1;

    long long int size = v[e].size();

    for(long long int k=0;k<size;k++)
    {
        long long int p = v[e][k];
        if(visited[p]==0)
        {
            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
                count1++;
            }

            dfs(p);

            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
                count1++;
            }
        }
    }

}
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int j=1;j<=n;j++)
        {
            if(array2[i][j]==1)
            {
                if(i<j)
                v[i].push_back(j);
                if(j<i)
                v[j].push_back(i);
            }
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
                if(array2[i][k]==1 && array2[k][h]==1)
                {
                    if(i<h)
                    {
                        v[i].push_back(h);
                    }
                    else if(h<i)
                    {
                        v[h].push_back(i);
                    }
                }
            }
        }
    }
    count1=1;
    for(long long int j=1;j<=n;j++)
    {
            memset(visited,0,sizeof(visited));
            dfs(j);
    }
    for(long long int j=1;j<=n;j++)
    {
        cout<<array1[j]<<" ";
    }
}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
                if(array2[i][h]==0)
                {
                if(array2[i][k]==1 && array2[k][h]==1 )
                {
                    array2[i][h]=1;
                }
                }
            }
        }
    }
        for(long long int k=1;k<=n;k++)
        {
          for(long long int l=k+1;l<=n;l++)
          {
            if(array2[k][l]==1 && array1[k] > array1[l])
            {
                long long int temp = array1[k];
                array1[k] = array1[l];
                array1[l] = temp;
            }
          }
        }

    for(long long int i=1;i<=n;i++)
    {
        cout<<array1[i]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
void dfs(long long int e)
{
    visited[e]=1;

    long long int size = v[e].size();

    for(long long int k=0;k<size;k++)
    {
        long long int p = v[e][k];
        if(visited[p]==0)
        {
            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
            }

            dfs(p);

            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
            }
        }
    }

}
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int j=1;j<=n;j++)
        {
            if(array2[i][j]==1)
            {
                if(i<j)
                v[i].push_back(j);
            }
        }
    }

    for(long long int j=1;j<=n;j++)
    {
        if(visited[j]==0)
        {
            dfs(j);
        }
    }

    for(long long int j=1;j<=n;j++)
    {
        cout<<array1[j]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
                if(array2[i][k]==1 && array2[k][h]==1)
                {
                    array2[i][h]=1;
                }
            }
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int j=1;j<=n;j++)
        {
            if(array2[i][j]==1)
            {
                v[i].push_back(j);
                v[j].push_back(i);
            }
        }
    }
    count1=1;
    while(1)
    {
        if(count1==0)
        break;
        else
        count1=0;

        for(long long int k=1;k<=n;k++)
        {
            long long int e = array1[k];

            long long int pos1 = pos[e];
            long long int size = v[pos1].size();
            for(long long int j=0;j<size;j++)
            {
                long long int pos2 = v[pos1][j];
                long long int e2 = array1[pos2];
                if(e2 > e && pos2 < pos1)
                {
                    long long int temp = array1[pos1];
                    array1[pos1] = array1[pos2];
                    pos[array1[pos2]] = pos1;
                    pos[temp] = pos2;
                    array1[pos2] = temp;
                    count1++;
                }
                else if(e2 < e && pos2 > pos1)
                {
                    long long int temp = array1[pos1];
                    array1[pos1] = array1[pos2];
                    pos[array1[pos2]] = pos1;
                    pos[temp] = pos2;
                    array1[pos2] = temp;
                    count1++;
                }

            }
        }

    }

    for(long long int i=1;i<=n;i++)
    {
        cout<<array1[i]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
void dfs(long long int e)
{
    visited[e]=1;

    long long int size = v[e].size();

    for(long long int k=0;k<size;k++)
    {
        long long int p = v[e][k];
        if(visited[p]==0)
        {
            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
                count1++;
            }

            dfs(p);

            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
                count1++;
            }
        }
    }

}
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int j=1;j<=n;j++)
        {
            if(array2[i][j]==1)
            {
                if(i<j)
                v[i].push_back(j);
                if(j<i)
                v[j].push_back(i);
            }
        }
    }
    count1=1;
    while(1)
    {
    if(count1==0)
    break;
    else count1=0;
    for(long long int j=1;j<=n;j++)
    {
            memset(visited,0,sizeof(visited));
            dfs(j);
    }
    }

    for(long long int j=1;j<=n;j++)
    {
        cout<<array1[j]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
void dfs(long long int e)
{
    visited[e]=1;

    long long int size = v[e].size();

    for(long long int k=0;k<size;k++)
    {
        long long int p = v[e][k];
        if(visited[p]==0)
        {
            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
                count1++;
            }

            dfs(p);

            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
                count1++;
            }
        }
    }

}
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int j=1;j<=n;j++)
        {
            if(array2[i][j]==1)
            {
                if(i<j)
                v[i].push_back(j);
                if(j<i)
                v[j].push_back(i);
            }
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
                if(array2[i][k]==1 && array2[k][h]==1)
                {
                    if(i<h)
                    {
                        v[i].push_back(h);
                    }
                    else if(h<i)
                    {
                        v[h].push_back(i);
                    }
                }
            }
        }
    }
    count1=1;
    for(long long int j=1;j<=n;j++)
    {
            //memset(visited,0,sizeof(visited));
            dfs(j);
    }
    for(long long int j=1;j<=n;j++)
    {
        cout<<array1[j]<<" ";
    }
}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
int main()
{
    long long int n;
    char a2[400];
    long long int array1[400];
    long long int array2[400][400];
    long long int pos[400];
    vector<long long int>v[400];
    cin>>n;
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }

    for(long long int i=1;i<=n;i++)
    {
        for(long long int j=1;j<=n;j++)
        {
            if(array2[i][j]==1)
            {
                v[i].push_back(j);
                v[j].push_back(i);
            }
        }
    }
    long long int count1=1;
    while(1)
    {
        if(count1!=0)
        count1=0;
        else if(count1==0)
        break;

        for(long long int k=1;k<=n;k++)
        {
            long long int e = array1[k];

            long long int pos1 = pos[e];
            long long int size = v[pos1].size();
            for(long long int j=0;j<size;j++)
            {
                long long int pos2 = v[pos1][j];
                long long int e2 = array1[pos2];
                if(e2 < e && pos2 > pos1)
                {
                    long long int temp = array1[pos1];
                    array1[pos1] = array1[pos2];
                    pos[array1[pos2]] = pos1;
                    pos[temp] = pos2;
                    array1[pos2] = temp;
                    count1++;
                }
                else if(e2 > e && pos2 < pos1)
                {
                    long long int temp = array1[pos1];
                    array1[pos1] = array1[pos2];
                    pos[array1[pos2]] = pos1;
                    pos[temp] = pos2;
                    array1[pos2] = temp;
                    count1++;
                }
            }
        }

    }

    for(long long int i=1;i<=n;i++)
    {
        cout<<array1[i]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
               a[k][h]=(a[k][h])|(a[k][i]&a[i][h]);
            }
        }
    }
        for(long long int k=1;k<=n;k++)
        {
          for(long long int l=k+1;l<=n;l++)
          {
            if(array2[k][l]==1 && array1[k] > array1[l])
            {
                long long int temp = array1[k];
                array1[k] = array1[l];
                array1[l] = temp;
            }
          }
        }

    for(long long int i=1;i<=n;i++)
    {
        cout<<array1[i]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
                if(array2[i][k]==1 && array2[k][h]==1)
                {
                    array2[i][h]=1;
                }
            }
        }
    }
        for(long long int k=1;k<=n;k++)
        {
          for(long long int l=k+1;l<=n;l++)
          {
            if(array2[k][l]==1 && array1[k] > array1[l])
            {
                long long int temp = array1[k];
                array1[k] = array1[l];
                array1[l] = temp;
            }
          }
        }

    for(long long int i=1;i<=n;i++)
    {
        cout<<array1[i]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
               array2[k][h]=(array2[k][h])|(array2[k][i]&array2[i][h]);
            }
        }
    }
        for(long long int k=1;k<=n;k++)
        {
          for(long long int l=k+1;l<=n;l++)
          {
            if(array2[k][l]==1 && array1[k] > array1[l])
            {
                long long int temp = array1[k];
                array1[k] = array1[l];
                array1[l] = temp;
            }
          }
        }

    for(long long int i=1;i<=n;i++)
    {
        cout<<array1[i]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
void dfs(long long int e)
{
    visited[e]=1;

    long long int size = v[e].size();

    for(long long int k=0;k<size;k++)
    {
        long long int p = v[e][k];
        if(visited[p]==0)
        {
            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
            }

            dfs(p);

            if(array1[p] < array1[e])
            {
                long long int temp = array1[p];
                array1[p] = array1[e];
                array1[e] = temp;
            }
        }
    }

}
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int j=1;j<=n;j++)
        {
            if(array2[i][j]==1)
            {
                if(i<j)
                v[i].push_back(j);
            }
        }
    }

    for(long long int j=1;j<=n;j++)
    {
            memset(visited,0,sizeof(visited));
            dfs(j);
    }

    for(long long int j=1;j<=n;j++)
    {
        cout<<array1[j]<<" ";
    }

}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<limits.h>
#include<stack>
#define pp pair<long long int,long long int>
using namespace std;
long long int n;
char a2[400];
long long int array1[400];
long long int array2[400][400];
long long int pos[400];
long long int visited[400];
vector<long long int>v[400];
long long int count1=0;
int main()
{
    cin>>n;
    memset(visited,0,sizeof(visited));
    for(long long int i=1;i<=n;i++)
    {
        cin>>array1[i];
        pos[array1[i]]=i;
    }
    for(long long int i=1;i<=n;i++)
    {
        cin>>a2;
        for(long long int j=1;j<=n;j++)
        {
            array2[i][j] = a2[j-1] - '0';
        }
    }
    for(long long int i=1;i<=n;i++)
    {
        for(long long int k=1;k<=n;k++)
        {
            for(long long int h=1;h<=n;h++)
            {
               if(array2[k][h]==0)
               {
                    if(array2[k][i]==1 && array2[i][h]==1)
                    array2[k][h]=1;
               }
            }
        }
    }
        for(long long int k=1;k<=n;k++)
        {
          for(long long int l=k+1;l<=n;l++)
          {
            if(array2[k][l]==1 && array1[k] > array1[l])
            {
                long long int temp = array1[k];
                array1[k] = array1[l];
                array1[l] = temp;
            }
          }
        }

    for(long long int i=1;i<=n;i++)
    {
        cout<<array1[i]<<" ";
    }

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,j,k,mn,pos;
    char c;
    cin>>n;
    int arr[n+1],b[n+1][n+1];
    for(i=0;i<n;i++) cin>>arr[i];
    for(i=0;i<n;i++) for(j=0;j<n;j++) {cin>>c; b[i][j]=c-'0';}
    for(i=0;i<n;i++)
    {
        mn=arr[i];
        pos=i;
        for(k=i+1;k<n;k++)
        {
            if(arr[k]<mn)
            {
                mn=arr[k];
                pos=k;
            }
        }
        if(b[i][pos]) swap(arr[i],arr[pos]);
        //else break;
    }
    for(i=0;i<n;i++) cout<<arr[i]<<" ";
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

#define mod 1000000007
#define ll long long int
#define si(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define pi(n) printf("%d",n)
#define pl(n) printf("%lld",n)
#define vi vector<int>
#define vli vector<long int>
#define vll vector<long long int>
#define pb push_back
#define mp make_pair
#define pr pair<long int,long int>
#define vpi vector< pair<int,int> >
#define vpl vector< pair<long long int,long long int> >
//ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b);}
//bool comp(int a,int b){return a>b;}
//string to_string(ll n){ ostringstream ss; ss<<n; return ss.str();} //number to string conversion

int main()
{
    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    int n,i,j,k,pos;
    si(n);
    char c;
    int a[n],b[n][n];
    for(i=0;i<n;i++) si(a[i]);
    for(i=0;i<n;i++) for(j=0;j<n;j++) {cin>>c; b[i][j]=c-'0';}
    for(k=0;k<n;k++) for(i=0;i<n;i++) for(j=0;j<n;j++) if(b[i][k]&&b[j][k]) b[i][j]=1;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(b[i][j]&&a[j]<a[i]) swap(a[i],a[j]);
        }
    }
    for(i=0;i<n;i++) {pi(a[i]); printf(" ");}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,j,mn,pos;
    char c;
    cin>>n;
    int arr[n+1],b[n+1][n+1];
    for(i=0;i<n;i++) cin>>arr[i];
    for(i=0;i<n;i++) for(j=0;j<n;j++) {cin>>c; b[i][j]=c-'0';}
    for(i=0;i<n-1;i++)
    {
         mn=arr[i];
         pos=i;
         for(j=i+1;j<n;j++)
         {
             if(arr[j]<mn) {mn=arr[j]; pos=j;}
             else continue;
         }
         if(b[arr[i]][mn])
         {
             swap(arr[i],arr[mn]);
         }
         else continue;
    }
    for(i=0;i<n;i++) cout<<arr[i]<<" ";
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

#define mod 1000000007
#define ll long long int
#define si(n) scanf("%d",&n)
//#define sl(n) scanf("%lld",&n)
#define pi(n) printf("%d",n)
//#define pl(n) printf("%lld",n)
#define vi vector<int>
#define vli vector<long int>
#define vll vector<long long int>
#define pb push_back
#define mp make_pair
#define pr pair<long int,long int>
#define vpi vector< pair<int,int> >
#define vpl vector< pair<long long int,long long int> >
//ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b);}
//bool comp(int a,int b){return a>b;}
//string to_string(ll n){ ostringstream ss; ss<<n; return ss.str();} //number to string conversion

int main()
{
    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    int n,i,j,k,pos;
    si(n);
    char c;
    int a[n],b[n][n];
    for(i=0;i<n;i++) si(a[i]);
    for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>c,b[i][j]=c-'0';
    for(k=0;k<n;k++) for(i=0;i<n;i++) for(j=0;j<n;j++) if(b[i][k]&&b[j][k]) b[i][j]=1;
    for(i=0;i<n;i++)
    {
        k=a[i];
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(a[j]<k)
            {
                k=a[j];
                pos=j;
            }
        }
        if(b[i][pos]) swap(a[i],a[pos]);
    }
    for(i=0;i<n;i++) pi(a[i]); printf(" ");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,j,k,mn,pos;
    char c;
    cin>>n;
    int arr[n+1],b[n+1][n+1];
    for(i=0;i<n;i++) cin>>arr[i];
    for(i=0;i<n;i++) for(j=0;j<n;j++) {cin>>c; b[i][j]=c-'0';}
    for(i=0;i<n;i++)
    {
        mn=arr[i];
        pos=i;
        for(k=i+1;k<n;k++)
        {
            if(arr[k]<mn)
            {
                mn=arr[k];
                pos=k;
            }
        }
        if(b[i][pos]) swap(arr[i],arr[pos]);
        else break;
    }
    for(i=0;i<n;i++) cout<<arr[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,j,mn,pos;
    char c;
    cin>>n;
    int arr[n+1],b[n+1][n+1];
    for(i=0;i<n;i++) cin>>arr[i];
    for(i=0;i<n;i++) for(j=0;j<n;j++) {cin>>c; b[i][j]=c-'0';}
    for(i=0;i<n-1;i++)
    {
         mn=arr[i];
         pos=i;
         for(j=i+1;j<n;j++)
         {
             if(arr[j]<mn) {mn=arr[j]; pos=j;}
             else continue;
         }
         if(b[i][pos]==1)
         {
             swap(arr[i],arr[pos]);
         }
         else continue;
    }
    for(i=0;i<n;i++) cout<<arr[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,j,mn,pos;
    char c;
    cin>>n;
    int arr[n+1],b[n+1][n+1];
    for(i=0;i<n;i++) cin>>arr[i];
    for(i=0;i<n;i++) for(j=0;j<n;j++) {cin>>c; b[i][j]=c-'0';}
    for(i=0;i<n-1;i++)
    {
         mn=arr[i];
         pos=i;
         for(j=i+1;j<n;j++)
         {
             if(arr[j]<mn) {mn=arr[j]; pos=j;}
             else continue;
         }
         if(b[i][mn])
         {
             swap(arr[i],arr[mn]);
         }
         else continue;
    }
    for(i=0;i<n;i++) cout<<arr[i]<<" ";
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

#define mod 1000000007
#define ll long long int
#define si(n) scanf("%d",&n)
//#define sl(n) scanf("%lld",&n)
#define pi(n) printf("%d",n)
//#define pl(n) printf("%lld",n)
#define vi vector<int>
#define vli vector<long int>
#define vll vector<long long int>
#define pb push_back
#define mp make_pair
#define pr pair<long int,long int>
#define vpi vector< pair<int,int> >
#define vpl vector< pair<long long int,long long int> >
//ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b);}
//bool comp(int a,int b){return a>b;}
//string to_string(ll n){ ostringstream ss; ss<<n; return ss.str();} //number to string conversion

int main()
{
    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    int n,i,j,k,pos;
    si(n);
    char c;
    int a[n],b[n][n];
    for(i=0;i<n;i++) si(a[i]);
    for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>c,b[i][j]=c-'0';
    for(k=0;k<n;k++) for(i=0;i<n;i++) for(j=0;j<n;j++) if(b[i][k]&&b[j][k]) b[i][j]=1;
    for(i=0;i<n;i++)
    {
        k=a[i];
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(a[j]<k)
            {
                k=a[j];
                pos=j;
            }
        }
        if(b[i][pos]) swap(a[i],a[pos]);
    }
    for(i=0;i<n;i++) {pi(a[i]); printf(" ");}
    return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
//typedef long long Long;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 300 + 5;
int ans[MAXN], pos[MAXN], ar[MAXN];
bool judge[MAXN][MAXN], vis[MAXN];
char temp[MAXN];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        for (int i = 1; i <= n; i++) {
            scanf("%d", &ar[i]);
            pos[ar[i]] = i;
        }
        for (int i = 1; i <= n; i++) {
            scanf("%s", temp + 1);
            for (int j = 1; j <= n; j++) {
                judge[i][j] = temp[j] - '0';
            }
            judge[i][i] = true;
        }
        //���ÿ������Ե�������е�
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= n; k++) {
                    if (judge[i][j] && judge[j][k]) {
                        judge[i][k] = true;
                    }
                }
            }
        }
        //i����ÿһ��λ��
        memset(vis, false, sizeof(vis));
        for (int i = 1; i <= n; i++) {
            //ÿ�ζ�����С�Ŀ�ʼ
            for (int j = 1; j <= n; j++) {
                if (!vis[j] && judge[i][pos[j]]) {
                    vis[j] = true; ans[i] = j;
                    //����λ��
                    pos[j] = i; pos[ar[i]] = pos[j];
                    break;
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i != n) printf("%d ",ans[i]);
            else printf("%d\n", ans[i]);
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 3*100 + 5;
int arr[310],pos[310],visited[310],ans[310];
char s[310][310];
int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        for (int i = 1; i <= n; i++) {
            scanf("%d",&arr[i]);
            pos[arr[i]] = i;
        }
        //�������ݴ����arr�У�����pos�м�¼i��ֵ���ֵ�λ��
        //������󣬽��Խ����ϵģ��Լ����Ժ��Լ���������ֵ��ʼ��Ϊ1
        for (int i = 1; i <= n; i++) {
            scanf("%s",s[i] + 1);
            s[i][i] = '1';
        }
        //Floyd�㷨 iΪ��ת��λ�ã��жϴ�jλ�õ�kλ��ͨ��iλ����ת�Ƿ����
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
				for (int k = 1; k <= n; k++) {
                    if(s[j][i] == '1' && s[k][i] == '1'){
                        s[j][k] = '1';
                    }
                }
            }
        }
        memset(visited, false, sizeof(visited));
        //��ans�����еĵ�һ��λ�ã�����ֵ1��ʼ�������ܽ���С��ֵ����ans��ǰ��
        for(int i = 1; i <= n; i++){
            //ѡ����ǰλ��i
            for(int j = 1; j <= n; j++){
                //��1��ʼ�жϣ�jֵ�Ƿ��Ѿ��ù������û�У����Ƿ���ܴ�����j���ڵ�λ�û�����ǰλ��i
                if(!visited[j] && s[i][pos[j]] == '1'){
                    ans[i] = j;
                    visited[j] = 1;
                    pos[j] = i;
                    pos[arr[i]] = pos[j];
                    break;
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i != n) printf("%d ",ans[i]);
            else printf("%d\n", ans[i]);
        }
    }
    return 0;
}


/*
˼·��
	���ǿ��Եõ�����һ����ʽ(x-y)=(n-m)t+Lp;
	����x-y��n-m��L����֪�����ֱ���ABC��ʾ �� A=Bt+Cp��
	�����ԣ���һ��t��p�����ʽ�ӡ�
	������չŷ����ã�A%(D=GCD(B,C)) != 0,�����޽⡣
	����н⣬���Ƚ��һ��t0��p0, ����B*t0*A/D+C*p0*A/D=A
	����ͬʱ��ȥA����B/D*t0+C/D*p0=1,����u*C*B/D-u*C*B/D=0�õ�
	B/D(t0+u*C)+C/D(p0-u*B)=1,���Եõ�t�Ľ�ϵ(t0+u*C) u����Z��
	����ʱ����һ���ڵ��������������ȷ����Сֵ
*/
#include <bits/stdc++.h>
#define MAXN 10005
using namespace std;
int extgcd(__int64 a, __int64 b, __int64 & x, __int64 & y) {
	int d = a;
	if (b != 0) {
		d = extgcd(b, a%b, y, x);
		y -= (a/b)*x;
	}
	else {
		x = 1, y = 0;
	}
	return d;
}
int main() {
	__int64 x, y, m, n, l, t0, p0;
	while (scanf("%I64d%I64d%I64d%I64d%I64d", &x, &y, &m, &n, &l) != EOF) {
		__int64 D = extgcd(abs(n-m), l, t0, p0);
		if ((x-y) % D != 0) {
			printf("Impossible\n");
		}
		else {
			t0 %= l; t0 = max(t0, -t0);
			printf("%I64d\n", t0);
		}
	}
	return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
//typedef long long Long;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 300 + 5;
int ans[MAXN], pos[MAXN], ar[MAXN];
bool judge[MAXN][MAXN], vis[MAXN];
char temp[MAXN];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        for (int i = 0; i < n; i++) {
            scanf("%d", &ar[i]);
            pos[ar[i]] = i;
        }
        for (int i = 0; i < n; i++) {
            scanf("%s", temp);
            for (int j = 0; j < n; j++) {
                judge[i][j] = temp[j] - '0';
            }
            judge[i][i] = true;
        }
        //���ÿ������Ե�������е�
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    if (judge[i][j] && judge[j][k]) {
                        judge[i][k] = true;
                    }
                }
            }
        }
        //i����ÿһ��λ��
        memset(vis, false, sizeof(vis));
        for (int i = 0; i < n; i++) {
            //ÿ�ζ�����С�Ŀ�ʼ
            for (int j = 1; j <= n; j++) {
                if (!vis[j] && judge[i][pos[j]]) {
                    vis[j] = true; ans[i] = j;
                    //����λ��
                    pos[j] = i; pos[ar[i]] = pos[j];
                    break;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if (i != n) printf("%d ",ans[i]);
            else printf("%d\n", ans[i]);
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
//typedef long long Long;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 300 + 10;
int ans[MAXN], pos[MAXN], ar[MAXN];
bool vis[MAXN];
char judge[MAXN][MAXN];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        for (int i = 1; i <= n; i++) {
            scanf("%d", &ar[i]);
            pos[ar[i]] = i;
        }
        for (int i = 1; i <= n; i++) {
            scanf("%s", judge[i] + 1);
            judge[i][i] = '1';
        }
        //���ÿ������Ե�������е�
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= n; k++) {
                    if (judge[i][j] == '1' && judge[j][k] == '1') {
                        judge[i][k] = '1';
                    }
                }
            }
        }
        //i����ÿһ��λ��
        memset(vis, false, sizeof(vis));
        for (int i = 1; i <= n; i++) {
            //ÿ�ζ�����С�Ŀ�ʼ
            for (int j = 1; j <= n; j++) {
                if (!vis[j] && judge[i][pos[j]] == '1') {
                    vis[j] = true; ans[i] = j;
                    //����λ��
                    pos[j] = i; pos[ar[i]] = pos[j];
                    break;
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i != n) printf("%d ",ans[i]);
            else printf("%d\n", ans[i]);
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 3*100 + 5;
bool judge[MAXN][MAXN];
char temp[MAXN];
int ar[MAXN];
int main() {
	int n, a, t;
	while (scanf("%d", &n) != EOF) {
		for (int i = 0; i < n; i++) {
			scanf("%d", &ar[i]);
		}
		for (int i = 0; i < n; i++) {
			scanf("%s", temp);
			for (int j = 0; j < n; j++) {
				judge[i][j] = temp[j] - '0';
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = i; k >= 0; k--) {
					if (ar[k] > ar[j] && judge[j][k]) {
						t = ar[j]; ar[j] = ar[k];
						ar[k] = t;
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			if (i == n - 1) printf("%d\n", ar[i]);
			else printf("%d ", ar[i]);
		}
	}
	return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 3*100 + 5;
bool judge[MAXN][MAXN];
char temp[MAXN];
int ar[MAXN];
int main() {
	int n, a, t;
	while (scanf("%d", &n) != EOF) {
		for (int i = 0; i < n; i++) {
			scanf("%d", &ar[i]);
		}
		for (int i = 0; i < n; i++) {
			scanf("%s", temp);
			for (int j = 0; j < n; j++) {
				judge[i][j] = temp[j] - '0';
			}
		}
		int mm = 3;
		while (mm--) {
			for (int i = 0; i < n; i++) {
				for (int j = i; j < n; j++) {
					if (ar[i] > ar[j] && judge[i][j]) {
						t = ar[i];
						ar[i] = ar[j];
						ar[j] = t;
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			if (i == n - 1) printf("%d\n", ar[i]);
			else printf("%d ", ar[i]);
		}
	}
	return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 3*100 + 5;
bool judge[MAXN][MAXN];
char temp[MAXN];
int ar[MAXN];
int main() {
	int n, a, t;
	while (scanf("%d", &n) != EOF) {
		for (int i = 0; i < n; i++) {
			scanf("%d", &ar[i]);
		}
		for (int i = 0; i < n; i++) {
			scanf("%s", temp);
			for (int j = 0; j < n; j++) {
				judge[i][j] = temp[j] - '0';
			}
		}
		for (int i = 0; i < n; i++) {
			for (int i = 0; i < n; i++) {
				for (int j = i; j < n; j++) {
					if (ar[i] > ar[j] && judge[i][j]) {
						t = ar[i];
						ar[i] = ar[j];
						ar[j] = t;
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			if (i == n - 1) printf("%d\n", ar[i]);
			else printf("%d ", ar[i]);
		}
	}
	return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 3*100 + 5;
int arr[310],pos[310],visited[310],ans[310];
char s[310][310];
int main(){
    int n;
    while(scanf("%d", &n)){
        for (int i = 1; i <= n; i++) {
            scanf("%d",&arr[i]);
            pos[arr[i]] = i;
        }
        //�������ݴ����arr�У�����pos�м�¼i��ֵ���ֵ�λ��
        //������󣬽��Խ����ϵģ��Լ����Ժ��Լ���������ֵ��ʼ��Ϊ1
        for (int i = 1; i <= n; i++) {
            scanf("%s",s[i] + 1);
            s[i][i] = '1';
        }
        //Floyd�㷨 iΪ��ת��λ�ã��жϴ�jλ�õ�kλ��ͨ��iλ����ת�Ƿ����
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
				for (int k = 1; k <= n; k++) {
                    if(s[j][i] == '1' && s[k][i] == '1'){
                        s[j][k] = '1';
                    }
                }
            }
        }
        memset(visited, false, sizeof(visited));
        //��ans�����еĵ�һ��λ�ã�����ֵ1��ʼ�������ܽ���С��ֵ����ans��ǰ��
        for(int i = 1; i <= n; i++){
            //ѡ����ǰλ��i
            for(int j = 1; j <= n; j++){
                //��1��ʼ�жϣ�jֵ�Ƿ��Ѿ��ù������û�У����Ƿ���ܴ�����j���ڵ�λ�û�����ǰλ��i
                if(!visited[j] && s[i][pos[j]] == '1'){
                    ans[i] = j;
                    visited[j] = 1;
                    pos[j] = i;
                    pos[arr[i]] = pos[j];
                    break;
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i != n) printf("%d ",ans[i]);
            else printf("%d\n", ans[i]);
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 3*100 + 5;
int ar[MAXN], pos[MAXN], ans[MAXN];
bool vis[MAXN];
char s[MAXN][MAXN];
int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        for (int i = 1; i <= n; i++) {
            scanf("%d",&ar[i]);
            pos[ar[i]] = i;
        }
        for (int i = 1; i <= n; i++) {
            scanf("%s",s[i] + 1);
            s[i][i] = '1';
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
				for (int k = 1; k <= n; k++) {
                    if(s[j][i] == '1' && s[k][i] == '1'){
                        s[j][k] = '1';
                    }
                }
            }
        }
        memset(vis, false, sizeof(vis));
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(!vis[j] && s[i][pos[j]] == '1'){
                    ans[i] = j;
                    vis[j] = 1; pos[j] = i;
                    pos[ar[i]] = pos[j];
                    break;
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i != n) printf("%d ",ans[i]);
            else printf("%d\n", ans[i]);
        }
    }
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N];
int n,i,j,k,a[MAX_N];
bool c[MAX_N][MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
      cin>>a[i];

    for(i=1;i<=n;i++)
       {
        cin>>(s+1);
        for(j=1;j<=n;j++)
          c[i][j]=s[j]-'0';
       }

    for(k=1;k<=n;k++)
     for(i=1;i<n;i++)
       for(j=i+1;j<=n;j++)
         if(c[i][j] && a[i]>a[j]) swap(a[i],a[j]);

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N];
int n,i,j,k,a[MAX_N];
bool c[MAX_N][MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
      cin>>a[i];

    for(i=1;i<=n;i++)
       {
        cin>>(s+1);
        for(j=1;j<=n;j++)
          c[i][j]=s[j]-'0';
       }

    for(k=1;k<=10*n;k++)
     for(i=1;i<n;i++)
       for(j=i+1;j<=n;j++)
         if(c[i][j] && a[i]>a[j]) swap(a[i],a[j]);

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N];
int n,i,j,k,a[MAX_N];
bool c[MAX_N][MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
      cin>>a[i];

    for(i=1;i<=n;i++)
       {
        cin>>(s+1);
        for(j=1;j<=n;j++)
          c[i][j]=s[j]-'0';
       }

    for(k=1;k<=n*n;k++)
     for(i=1;i<n;i++)
       for(j=i+1;j<=n;j++)
         if(c[i][j] && a[i]>a[j]) swap(a[i],a[j]);

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N][MAX_N];
int n,i,j,k,a[MAX_N],poz[MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
     { cin>>a[i]; poz[a[i]]=i; }

    for(i=1;i<=n;i++)
      cin>>(s[i]+1);

    t=1;
    while(t){ t=0;
    for(k=1;k<=n;k++)
        {

         int p=-1; int mm=k;
         for(j=poz[k]-1;j>=1;--j)
           if(s[poz[k]][j]-'0' && k<a[j]) p=j,mm=a[j];

         if(p!=-1){
                   //cout<<p<<" "<<poz[k]<<endl;

                   int poz1=p;
                   int poz2=poz[k];
                   int x1=a[poz1];
                   int x2=a[poz2];

                   a[poz1]=x2;
                   a[poz2]=x1;

                   poz[a[poz1]]=poz1;
                   poz[a[poz2]]=poz2;

                   //for(int q=1;q<=n;q++) cout<<a[q]<<" ";cout<<endl;
                   //for(int q=1;q<=n;q++) cout<<poz[q]<<" "; cout<<endl;
                   t=1;

                  }
        }
   }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    //system("pause");
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N][MAX_N];
int n,i,j,k,a[MAX_N],poz[MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
     { cin>>a[i]; poz[a[i]]=i; }

    for(i=1;i<=n;i++)
      cin>>(s[i]+1);

    t=1;
    while(t){ t=0;
    for(k=1;k<=n;k++)
        {

         int p=-1;
         for(j=poz[k]-1;j>=1;--j)
           if(s[poz[k]][j]-'0' && k<a[j]) p=j;

         if(p!=-1){
                   //cout<<p<<" "<<poz[k]<<endl;
                   swap(a[p],a[poz[k]]);
                   poz[a[p]]=poz[k];
                   poz[k]=p;
                   t=1;
                  }
        }
   }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    //system("pause");
    return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N][MAX_N];
int n,i,j,a[MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
      cin>>a[i];

    for(i=1;i<=n;i++)
      cin>>(s[i]+1);

    t=1;
    while(t)
    {
     t=0;
     for(i=1;i<n;i++)
       for(j=i+1;j<=n;j++)
         if(a[i]>a[j] && (s[i][j]-'0')){ t=1; swap(a[i],a[j]); }
    }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    //system("pause");
    return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAX_N = 305;

int n,i,j,k,a[MAX_N];
bool c[MAX_N][MAX_N];
char s[MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
      cin>>a[i];

    for(i=1;i<=n;i++)
       {
        cin>>(s+1);
        for(j=1;j<=n;j++)
          c[i][j]=s[j]-'0';
       }

    for(k=1;k<=n;k++)
      for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
          c[i][j] = c[i][j] || (c[i][k] && c[k][j]);

    t=1;
    while(t){
             t=0;
             for(i=1;i<n;i++)
               for(j=i+1;j<=n;j++)
                 if(c[i][j] && a[i]>a[j]){
                                          t=0;
                                          swap(a[i],a[j]);
                                         }
            }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N];
int n,i,j,k,a[MAX_N];
bool c[MAX_N][MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
      cin>>a[i];

    for(i=1;i<=n;i++)
       {
        cin>>(s+1);
        for(j=1;j<=n;j++)
          c[i][j]=s[j]-'0';
       }

    for(k=1;k<=2*n;k++)
     for(i=1;i<n;i++)
       for(j=i+1;j<=n;j++)
         if(c[i][j] && a[i]>a[j]) swap(a[i],a[j]);

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N];
int n,i,j,k,a[MAX_N],poz[MAX_N];
bool c[MAX_N][MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
     { cin>>a[i]; poz[a[i]]=i; }

    for(i=1;i<=n;i++)
       {
        cin>>(s+1);
        for(j=1;j<=n;j++)
          c[i][j]=s[j]-'0';
       }

    for(k=1;k<=n;k++)
      for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
          c[i][j]=max(c[i][j],(c[i][k]-'0')||(c[k][j]));

    t=1;
    while(t)
    {
     t=0;
     for(i=1;i<n && !t;i++)
       for(j=i+1;j<=n && !t;j++)
         if(c[i][j] && a[i]>a[j]){
                                  swap(a[i],a[j]);
                                  t=1;
                                 }
    }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N];
int n,i,j,k,a[MAX_N],poz[MAX_N];
bool c[MAX_N][MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
     { cin>>a[i]; poz[a[i]]=i; }

    for(i=1;i<=n;i++)
       {
        cin>>(s+1);
        for(j=1;j<=n;j++)
          c[i][j]=s[j]-'0';
       }

    for(k=1;k<=n;k++)
      for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
          c[i][j]=max(c[i][j],(c[i][k])&&(c[k][j]));

    t=1;
    while(t)
    {
     t=0;
     for(i=1;i<n;i++)
       for(j=i+1;j<=n;j++)
         if(c[i][j] && a[i]>a[j]){
                                  swap(a[i],a[j]);
                                  t=1;
                                 }
    }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N][MAX_N];
int n,i,j,k,a[MAX_N],poz[MAX_N];
int t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
     { cin>>a[i]; poz[a[i]]=i; }

    for(i=1;i<=n;i++)
      cin>>(s[i]+1);

    t=0;
    while(t<=n){ t++;
    for(k=1;k<=n;k++)
        {

         int p=-1; int mm=k;
         for(j=poz[k]-1;j>=1;--j)
           if(s[poz[k]][j]-'0' && k<a[j]) p=j,mm=a[j];

         if(p!=-1){
                   //cout<<p<<" "<<poz[k]<<endl;
                   swap(a[p],a[poz[k]]);
                   poz[k]=p;
                   poz[a[p]]=poz[k];
                   //t=1;
                  }
        }
   }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    //system("pause");
    return 0;
}


#include<iostream>
//#include<fstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//ifstream cin("fisier.in");
//ofstream cout("fisier.out");
const int MAX_N = 305;

char s[MAX_N][MAX_N];
int n,i,j,k,a[MAX_N],poz[MAX_N];
bool t;

int main(){
    cin>>n;
    for(i=1;i<=n;i++)
     { cin>>a[i]; poz[a[i]]=i; }

    for(i=1;i<=n;i++)
      cin>>(s[i]+1);

    t=1;
    while(t){ t=0;
    for(k=1;k<=n;k++)
        {

         int p=-1; int mm=k;
         for(j=poz[k]-1;j>=1;--j)
           if(s[poz[k]][j]-'0' && k<a[j]) p=j,mm=a[j];

         if(p!=-1){
                   //cout<<p<<" "<<poz[k]<<endl;

                   int aux=poz[k];
                   poz[k]=p;
                   poz[a[p]]=aux;
                   swap(a[p],a[poz[a[p]]]);
                   //for(int q=1;q<=n;q++) cout<<a[q]<<" ";cout<<endl;
                   //for(int q=1;q<=n;q++) cout<<poz[q]<<" "; cout<<endl;
                   t=1;

                  }
        }
   }

    for(i=1;i<=n;i++)
      cout<<a[i]<<" ";

    //cin.close();
    //cout.close();
    //system("pause");
    return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&G[cur][j]=='1')
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    int cnt=0,pos;
    while(cnt<n)
    {
      int m=inf;
      for(int i=cnt+1;i<=n;i++)
      {
        if(a[i]<m)
        {
          m=a[i];
          pos=i;
        }
      }
      cnt++;
      gao(pos);
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
//void gao(int pos)
//{
//  int cur=pos;
//  while(1)
//  {
//    int flag=0;
//    for(int j=1;j<cur;j++)
//    {
//      if(a[cur]<a[j]&&G[cur][j]=='1')
//      {
//        //printf("%d %d\n",cur,j);
//        swap(a[cur],a[j]);
//        //debug();
//        cur=j;
//        flag=1;
//        break;
//      }
//    }
//    if(!flag)break;
//  }
//}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    for(int i=1;i<=n;i++)
    {
      for(int j=i+1;j<=n;j++)
      {
        if(a[j]<a[i]&&G[j][i]=='1')
        {
          swap(a[j],a[i]);
        }
      }
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&G[cur][j]=='1')
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    int cnt=1,pos;
    while(cnt<=n)
    {
      for(int i=1;i<=n;i++)
      {
        if(a[i]==cnt)
        {
          pos=i;
          break;
        }
      }
      cnt++;
      gao(pos);
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&G[cur][j]=='1')
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    for(int x=1;x<=5;x++)
    {
    int cnt=1,pos;
    while(cnt<=n)
    {
      for(int i=1;i<=n;i++)
      {
        if(a[i]==cnt)
        {
          pos=i;
          break;
        }
      }
      cnt++;
      gao(pos);
    }
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
int can[305][305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=n;j++)
    {
      printf("%c",G[i][j]);
    }
    printf("\n");
  }
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&can[cur][j])
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    for(int k=1;k<=n;k++)
    {
      for(int i=1;i<=n;i++)
      {
        for(int j=1;j<=n;j++)
        {
          G[i][j]=(G[i][j]=='1')||(G[i][k]=='1'&&G[k][j]=='1');
          G[i][j]+='0';
        }
      }
    }
    for(int i=1;i<=n;i++)
    {
      for(int j=i+1;j<=n;j++)
      {
        if(a[i]>a[j]&&G[i][j]=='1')
        {
          swap(a[i],a[j]);
        }
      }
    }
//    int cnt=1,pos;
//    while(cnt<=n)
//    {
//      for(int i=1;i<=n;i++)
//      {
//        if(a[i]==cnt)
//        {
//          pos=i;
//          break;
//        }
//      }
//      cnt++;
//      gao(pos);
//    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
    //debug();
  }
  return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
int can[305][305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&can[cur][j])
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    memset(can,0,sizeof(can));
    for(int k=1;k<=n;k++)
    {
      for(int i=1;i<=n;i++)
      {
        for(int j=1;j<=n;j++)
        {
          can[i][j]=G[i][j]-'0';
          if(G[i][k]=='1'&&G[k][j]=='1')
          {
            can[i][j]=can[j][i]=1;
          }
        }
      }
    }
    int cnt=1,pos;
    while(cnt<=n)
    {
      for(int i=1;i<=n;i++)
      {
        if(a[i]==cnt)
        {
          pos=i;
          break;
        }
      }
      cnt++;
      gao(pos);
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&G[cur][j]=='1')
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    int cnt=1,pos;
    while(cnt<=n)
    {
      for(int i=1;i<=n;i++)
      {
        if(a[i]==cnt)
        {
          pos=i;
          break;
        }
      }
      cnt++;
      gao(pos);
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
  }


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&G[cur][j]=='1')
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    for(int x=1;x<=10;x++)
    {
    int cnt=1,pos;
    while(cnt<=n)
    {
      for(int i=1;i<=n;i++)
      {
        if(a[i]==cnt)
        {
          pos=i;
          break;
        }
      }
      cnt++;
      gao(pos);
    }
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
}


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
char G[305][305];
int a[305];
int can[305][305];
const int inf=0x7ffffff;
void debug()
{
  for(int i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n");
}
void gao(int pos)
{
  int cur=pos;
  while(1)
  {
    int flag=0;
    for(int j=1;j<cur;j++)
    {
      if(a[cur]<a[j]&&can[cur][j])
      {
        //printf("%d %d\n",cur,j);
        swap(a[cur],a[j]);
        //debug();
        cur=j;
        flag=1;
        break;
      }
    }
    if(!flag)break;
  }
}
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
      scanf("%s",G[i]+1);
    }
    memset(can,0,sizeof(can));
    for(int k=1;k<=n;k++)
    {
      for(int i=1;i<=n;i++)
      {
        for(int j=1;j<=n;j++)
        {
          can[i][j]=can[j][i]=G[i][j]-'0';
          if(G[i][k]=='1'&&G[k][j]=='1')
          {
            can[i][j]=can[j][i]=1;
          }
        }
      }
    }
    int cnt=1,pos;
    while(cnt<=n)
    {
      for(int i=1;i<=n;i++)
      {
        if(a[i]==cnt)
        {
          pos=i;
          break;
        }
      }
      cnt++;
      gao(pos);
    }
    for(int i=1;i<=n;i++)
    {
      printf("%d",a[i]);
      if(i<n)printf(" ");
      else printf("\n");
    }
  }
  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=1;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
struct s
{
    int a;
}ss[305];
bool cmp(s x , s y)
{
    return x.a<y.a;
}
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=i+1;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
        if(num[i]==i)
        {
            for(int j=i;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++].a = a[j];
                }
            }
            sort(ss,ss+top,cmp);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j].a;
            top = 0;
        }
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=0;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
int ss[305];
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=i+1;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
            for(int j=0;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++] = a[j];
                }
            }
            sort(ss,ss+top);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j];
            top = 0;
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=1;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
struct s
{
    int a;
}ss[305];
bool cmp(s x , s y)
{
    return x.a<y.a;
}
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=0;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
        if(num[i]==i)
        {
            for(int j=0;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++].a = a[j];
                }
            }
            sort(ss,ss+top,cmp);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j].a;
            top = 0;
        }
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=1;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
struct s
{
    int a;
}ss[305];
bool cmp(s x , s y)
{
    return x.a<y.a;
}
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=0;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
        if(num[i]==i)
        {
            for(int j=i+1;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++].a = a[j];
                }
            }
            sort(ss,ss+top,cmp);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j].a;
            top = 0;
        }
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=1;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    num[xx]=yy;
}
char M[305][305];
int a[305];
struct s
{
    int a;
}ss[305];
bool cmp(s x , s y)
{
    return x.a<y.a;
}
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=0;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
        if(num[i]==i)
        {
            for(int j=0;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++].a = a[j];
                }
            }
            sort(ss,ss+top,cmp);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j].a;
            top = 0;
        }
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=1;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
struct s
{
    int a;
}ss[305];
bool cmp(s x , s y)
{
    return x.a<y.a;
}
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=0;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
        if(num[i]==i)
        {
            for(int j=i;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++].a = a[j];
                }
            }
            sort(ss,ss+top,cmp);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j].a;
            top = 0;
        }
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=1;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
int ss[305];
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=i+1;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
        if(num[i]==i)
        {
            for(int j=i;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++] = a[j];
                }
            }
            sort(ss,ss+top);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j];
            top = 0;
        }
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=0;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
int ss[305];
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=i+1;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
            for(int j=0;j<T;j++)
            {
                if(findset(j)==i)
                {
                    bb[top] = j;
                    ss[top++] = a[j];
                }
            }
            sort(ss,ss+top);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j];
            top = 0;
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
//#define LOCAL
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int num [305];
int init()
{
    for(int i=1;i<=300;i++)
        num[i]=i;
}
int findset(int x)
{
    if(x!=num[x])
        num[x]=findset(num[x]);
    return num[x];
}
int unionset(int x,int y)
{
    int xx = findset(x);
    int yy = findset(y);
    if(xx==yy)
        return 0;
    else if(xx<yy)
        num[yy]=xx;
    else num[xx]=yy;
}
char M[305][305];
int a[305];
int ss[305];
int bb[305];
int main()
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    init();
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<T;i++)
    {
        scanf("%s",M[i]);
        for(int j=i+1;j<T;j++)
        {
            if(i==j)
                continue;
            else if(M[i][j]=='1')
                unionset(i,j);
        }
    }
//    for(int i=0;i<T;i++)
//    printf("%d ",num[i]);
//    printf("\n");
    int vis[305];
    int top = 0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<T;i++)
    {
            for(int j=0;j<T;j++)
            {
                if(num[j]==i)
                {
                    bb[top] = j;
                    ss[top++] = a[j];
                }
            }
            sort(ss,ss+top);
//            for(int j=0;j<top;j++)
//                printf("%d\n",ss[j].a);
            for(int j=0;j<top;j++)
                a[bb[j]] = ss[j];
            top = 0;
    }
    for(int i=0;i<T;i++)
        if(i==0)
            printf("%d",a[i]);
        else printf(" %d",a[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

/*
 * You got a dream, you gotta protect it.
 */

const int N=300;
vector<int>g[N];
int n,v[N],idx[N],vis[N],id,low,SRC;
vector<int>res,temp;

void DFS(int v){
    if(v<low){
        low=v;
        res=temp;
    }
    vis[v]=id;
    for(size_t a=0;a<g[v].size();++a){
        int u=g[v][a];
        if(vis[u]==id||u<SRC)
            continue;
        temp.push_back(u);
        DFS(u);
        temp.pop_back();
    }
}

int main(){
//  freopen("read.txt","r",stdin);
    scanf("%d",&n);
    for(int a=0;a<n;++a){
        scanf("%d",v+a);
        --v[a];
        idx[v[a]]=a;
    }
    for(int a=0;a<n;++a)
        for(int b=0;b<n;++b){
            char ch;
            scanf(" %c",&ch);
            if(ch=='1')
                g[a].push_back(b);
        }
    for(int a=0;a<n;++a){
        ++id;
        SRC=a,low=idx[a];
        res.clear();
        temp=vector<int>(1,idx[a]);
        DFS(idx[a]);
        for(size_t a=1;a<res.size();++a)
            swap(v[res[a]],v[res[a-1]]);
        for(int a=0;a<n;++a)
            idx[v[a]]=a;
    }
    for(int a=0;a<n;++a)
        printf("%d ",v[a]+1);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

/*
 * You got a dream, you gotta protect it.
 */

const int N=300;
int n,arr[N],vis[N],idx[N],id;
vector<int>g[N],mv;
pair<int,vector<int> >res;

void DFS(int v,int w){
    vis[v]=id;
    mv.push_back(v);
    res=min(res,make_pair(v,mv));
    for(size_t a=0;a<g[v].size();++a){
        int u=g[v][a];
        if(vis[u]==id||arr[u]<w)
            continue;
        DFS(u,w);
    }
    mv.pop_back();
}

int main(int argc, char **argv){
//  freopen("a","r",stdin);
    scanf("%d",&n);
    for(int a=0;a<n;++a){
        scanf("%d",arr+a);
        --arr[a];
        idx[arr[a]]=a;
    }
    for(int a=0;a<n;++a)
        for(int b=0;b<n;++b){
            char ch;
            scanf(" %c",&ch);
            if(ch=='1')
                g[a].push_back(b);
        }
    for(int a=0;a<n;++a){
        ++id;
        res=make_pair(idx[a],vector<int>(1,2e9));
        DFS(idx[a],a);
        for(size_t a=1;a<res.second.size();++a)
            swap(arr[res.second[a]],arr[res.second[a-1]]);
        for(int a=0;a<n;++a)
            idx[arr[a]]=a;
    }
    for(int a=0;a<n;++a)
        printf("%d ",arr[a]+1);
    puts("");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

/*
 * You got a dream, you gotta protect it.
 */

const int N=300;
vector<int>g[N];
int n,v[N],idx[N],vis[N],id,low,SRC;
vector<int>res,temp;

void DFS(int v){
    if(v<low){
        low=v;
        res=temp;
    }
    vis[v]=id;
    for(size_t a=0;a<g[v].size();++a){
        int u=g[v][a];
        if(vis[u]==id||u<SRC||SRC>::v[u])
            continue;
        temp.push_back(u);
        DFS(u);
        temp.pop_back();
    }
}

int main(){
//  freopen("read.txt","r",stdin);
    scanf("%d",&n);
    for(int a=0;a<n;++a){
        scanf("%d",v+a);
        --v[a];
        idx[v[a]]=a;
    }
    for(int a=0;a<n;++a)
        for(int b=0;b<n;++b){
            char ch;
            scanf(" %c",&ch);
            if(ch=='1')
                g[a].push_back(b);
        }
    for(int a=0;a<n;++a){
        ++id;
        SRC=a,low=idx[a];
        res.clear();
        temp=vector<int>(1,idx[a]);
        DFS(idx[a]);
        for(size_t a=1;a<res.size();++a)
            swap(v[res[a]],v[res[a-1]]);
        for(int a=0;a<n;++a)
            idx[v[a]]=a;
    }
    for(int a=0;a<n;++a)
        printf("%d ",v[a]+1);
    return 0;
}