#include <bits/stdc++.h>
using namespace std;
int n, it, x, a[333], w[333];
char s[333][333];
bool u[333];
void dfs(int i) {
  if (!u[a[i]]) x = min(x, a[i]);
  w[i] = it;
  for (int j = 0; j < n; j++)
    if (s[i][j] == '1' && w[j] != it) dfs(j);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) {
    it++;
    x = n;
    dfs(i);
    u[x] = true;
    cout << x << " ";
  }
  return 0;
}
