#include <bits/stdc++.h>
using namespace std;
char a[310][310];
int p[310];
int pos[310];
bool done[310];
int op[310];
bool reach[310][310];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++) scanf("%s", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) reach[i][j] = (a[i][j - 1] == '1');
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) reach[i][j] |= reach[i][k] & reach[k][j];
  for (int i = 1; i <= n; i++) pos[p[i]] = i;
  for (int i = 1; i <= n; i++) {
    if (done[p[i]]) continue;
    done[p[i]] = 1;
    vector<int> cyc;
    vector<int> ids;
    for (int j = 1; j <= n; j++)
      if (reach[i][j] && i != j) {
        done[p[j]] = 1;
        cyc.push_back(p[j]);
        ids.push_back(j);
      }
    cyc.push_back(p[i]);
    ids.push_back(i);
    sort(cyc.begin(), cyc.end());
    sort(ids.begin(), ids.end());
    for (int j = 0; j < ids.size(); j++) op[ids[j]] = cyc[j];
  }
  for (int i = 1; i <= n; i++) printf("%d ", op[i]);
  return 0;
}
