#include <bits/stdc++.h>
using namespace std;
template <class T>
T bigmod(T a, T b) {
  T ans = 1;
  T base = a % 1000000007LL;
  while (b) {
    if (b & 1) ans = (ans * base) % 1000000007LL;
    base = (base * base) % 1000000007LL;
    b /= 2;
  }
  return ans;
}
template <class T>
T Bitcnt(T a) {
  int sum = 0;
  while (a) {
    if (a & 1) sum++;
    a /= 2;
  }
  return sum;
}
template <class T>
T Max3(T a, T b, T c) {
  return max(a, max(b, c));
}
int a_x[] = {1, -1, 0, 0};
int a_y[] = {0, 0, 1, -1};
vector<int> lst, res;
string grid[5000 + 10];
int n;
bool visit[5000 + 10];
void dfs(int nd) {
  lst.push_back(nd);
  visit[nd] = 1;
  for (int i = 0; i < n; i++) {
    if (grid[nd][i] == '1') {
      if (visit[i] == 0) {
        dfs(i);
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  int a[n + 10];
  int pos[n + 10];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }
  for (int i = 0; i < n; i++) cin >> grid[i];
  memset(visit, 0, sizeof visit);
  for (int i = 0; i < n; i++) {
    if (visit[i] == 0) {
      lst.clear();
      res.clear();
      dfs(i);
      for (int j = 0; j < lst.size(); j++) res.push_back(a[lst[j]]);
      sort(lst.begin(), lst.end());
      sort(res.begin(), res.end());
      for (int j = 0; j < lst.size(); j++) a[lst[j]] = res[j];
      lst.clear();
      res.clear();
    }
  }
  for (int i = 0; i < n; i++) cout << a[i] + 1 << " ";
  printf("\n");
  return 0;
}
