#include <bits/stdc++.h>
using namespace std;
vector<int> m, u;
int n;
int a[1111];
char s[500][500];
bool vis[1111];
void dfs(int i) {
  if (vis[i]) return;
  vis[i] = 1;
  m.push_back(i);
  u.push_back(a[i]);
  for (int j = 1; j <= n; j++) {
    if (s[i][j - 1] == '1') dfs(j);
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      sort(m.begin(), m.end());
      sort(u.begin(), u.end());
      for (int j = 1; j <= m.size(); j++) {
        a[m[j - 1]] = u[j - 1];
      }
      m.clear();
      u.clear();
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << a[i] << ' ';
  }
  cout << endl;
  return 0;
}
