#include <bits/stdc++.h>
using namespace std;
int visit[3000];
vector<long long> g[3000];
vector<int> t, pos;
int p[300];
void dfs(int node) {
  visit[node] = 1;
  t.push_back(p[node]);
  for (int i = 0; i < g[node].size(); i++) {
    if (!visit[g[node][i]]) {
      dfs(g[node][i]);
    }
  }
}
int main() {
  long long i, n, hash[301];
  char a[300][300];
  string s;
  cin >> n;
  int res[n];
  for (i = 0; i < n; i++) {
    cin >> p[i];
    hash[p[i]] = i;
  }
  for (i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      a[i][j] = s[j];
      a[j][i] = s[j];
      if (s[j] == '1') {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (!visit[i]) {
      t.clear();
      pos.clear();
      dfs(i);
      for (int j = 0; j < t.size(); j++) {
        pos.push_back(hash[t[j]]);
      }
      sort(pos.begin(), pos.end());
      sort(t.begin(), t.end());
      for (int j = 0; j < t.size(); j++) {
        res[pos[j]] = t[j];
      }
    }
  }
  for (i = 0; i < n; i++) cout << res[i] << " ";
  return 0;
}
