#include <bits/stdc++.h>
using namespace std;
int n, cnt, p[305], vis[305];
vector<int> g[305];
void dfs(int x) {
  vis[x] = cnt;
  for (int i = 0; i < g[x].size(); i++)
    if (!vis[g[x][i]]) dfs(g[x][i]);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> p[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      if (c == '1') g[i].push_back(j);
    }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      cnt++;
      dfs(i);
    }
  for (int i = 1; i <= cnt; i++) {
    vector<int> ind;
    for (int j = 1; j <= n; j++)
      if (vis[j] == i) ind.push_back(j);
    vector<int> num;
    for (int j = 0; j < ind.size(); j++) num.push_back(p[ind[j]]);
    sort(num.begin(), num.end());
    for (int j = 0; j < ind.size(); j++) p[ind[j]] = num[j];
  }
  for (int i = 1; i <= n; i++) cout << p[i] << ' ';
  cout << endl;
  return 0;
}
