#include <bits/stdc++.h>
using namespace std;
long long z = 1000000007;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long power(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a);
      b--;
    } else {
      a = (a * a);
      b = b >> 1;
    }
  }
  return res;
}
long long ar[301];
vector<long long> v[301];
long long vis[301] = {0};
void bfs(long long root) {
  long long a;
  queue<long long> q;
  q.push(root);
  set<long long> s1, s2;
  s1.insert(root);
  s2.insert(ar[root]);
  while (!q.empty()) {
    a = q.front();
    q.pop();
    vis[a] = 1;
    for (long long i = 0; i < v[a].size(); i++) {
      if (!vis[v[a][i]] && s1.find(v[a][i]) == s1.end()) {
        s2.insert(ar[v[a][i]]);
        s1.insert(v[a][i]);
        q.push(v[a][i]);
      }
    }
  }
  while (!s1.empty()) {
    long long i = *s1.begin(), val = *s2.begin();
    ar[i] = val;
    s1.erase(i);
    s2.erase(val);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  while (t--) {
    long long n;
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      cin >> ar[i];
    }
    char c[n][n];
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        cin >> c[i][j];
        if (c[i][j] == '1') {
          v[i + 1].push_back(j + 1);
          v[j + 1].push_back(i + 1);
        }
      }
    }
    for (long long i = 1; i <= n; i++)
      if (!vis[i]) bfs(i);
    for (long long i = 1; i <= n; i++) cout << ar[i] << " ";
    cout << "\n";
  }
}
