#include <bits/stdc++.h>
using namespace std;
ifstream fin("frac1.in");
ofstream fout("frac1.out");
class compare : public std::binary_function<double, double, bool> {
 public:
  compare(double arg = 1e-7) : e(arg) {}
  bool operator()(const double &left, const double &right) const {
    return (abs(left - right) > e) && (left < right);
  }
  double e;
};
inline int d_compare(double x, double y) {
  return (fabs(x - y) <= 1e-7) ? 0 : (x > y) ? 1 : -1;
}
inline int mod(long long x, int y) {
  if (y == 0) return x;
  if (x < 0) x = -x;
  if (x >= y) x %= y;
  return x;
}
inline int cmod(int x, int y) {
  if (y == 0) return x;
  if (x < 0) x += (abs(x) / y + 1) * y;
  if (x >= y) x = x % y;
  return x;
}
int Bcount(int mask) {
  int n = 0;
  while (mask) n += ((mask & -mask) != 0), mask &= ~(mask & -mask);
  return n;
}
inline bool Bstate(int mask, int idx) { return (mask & (1 << idx)); }
inline int B1(int mask, int idx) { return mask | (1 << idx); }
inline int B0(int mask, int idx) { return mask & ~(1 << idx); }
inline int drift(int mask, int n) { return mask & ((1 << n) - 1); }
void debugmask(int mask, int n) {
  for (int i = 0; i < n; i++) cout << Bstate(mask, i);
}
int dr[4] = {0, 0, -1, 1}, dc[4] = {-1, 1, 0, 0};
template <typename Ty>
void clear(Ty &q) {
  Ty empty;
  swap(q, empty);
}
int main() {
  int n;
  int p[300];
  int arr[300][300];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> p[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      arr[i][j] = c - '0';
    }
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (i != j)
          if (arr[i][k] && arr[k][j]) arr[i][j] = 1;
  bool flag = true;
  while (flag) {
    flag = false;
    for (int i = 0; i < n; i++) {
      int mini = i;
      for (int j = i + 1; j < n; j++)
        if (arr[i][j] && p[j] < p[mini]) mini = j;
      if (mini != i) flag = true, swap(p[i], p[mini]);
    }
  }
  for (int i = 0; i < n; i++) cout << p[i] << ' ';
  cout << endl;
  return 0;
}
