#include <bits/stdc++.h>
using namespace std;
const int maxn = 310;
int can[maxn][maxn];
int p[maxn];
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> p[i];
  char k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> k;
      can[i][j] = k - '0';
    }
  for (int i = 1; i <= n; i++) can[i][i] = 1;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (can[i][k] && can[k][j]) can[i][j] = 1;
  for (int i = 1; i < n; i++)
    for (int j = i + 1; j <= n; j++)
      if (p[i] > p[j] && can[i][j]) swap(p[i], p[j]);
  for (int i = 1; i <= n; i++) cout << p[i] << " ";
  cout << endl;
  return 0;
}
