#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
long long pow_mod(long long a, long long n, long long m) {
  if (n == 0) return 1;
  long long x = pow_mod(a, n >> 1, m);
  long long ans = x * x % m;
  if (n & 1) ans = ans * a % m;
  return ans;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int arr[310], pos[310], visited[310], ans[310];
char s[310][310];
int main() {
  int n;
  while (cin >> n) {
    for (int i = (1); i <= (n); i++) {
      scanf("%d", &arr[i]);
      pos[arr[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(visited, 0, sizeof(visited));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        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++) {
      printf("%d ", ans[i]);
    }
    cout << endl;
  }
  return 0;
}
