#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int> > v(400);
vector<long long int> v1;
vector<long long int> v2;
long long int vis[400];
long long int a[400];
void dfs(long long int x) {
  vis[x] = true;
  v1.push_back(x);
  v2.push_back(a[x]);
  for (long long int i = 0; i < v[x].size(); i++) {
    if (!vis[v[x][i]]) dfs(v[x][i]);
  }
}
int main() {
  long long int n, i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      char x;
      cin >> x;
      if (x == '1') v[i].push_back(j);
    }
  }
  memset(vis, false, sizeof(vis));
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      v1.clear();
      v2.clear();
      dfs(i);
      sort(v1.begin(), v1.end());
      sort(v2.begin(), v2.end());
      for (j = 0; j < v1.size(); j++) {
        a[v1[j]] = v2[j];
      }
    }
  }
  for (i = 1; i <= n; i++) cout << a[i] << " ";
  cout << endl;
}
