#include <bits/stdc++.h>
using namespace std;
string s;
int a[305], ans[305], b[305][305], n, mx = 0;
bool visit[305];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      b[i][j + 1] = s[j] - 48;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (b[i][k] && b[k][j]) b[i][j] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    mx = a[i];
    for (int j = 1; j <= n; j++) {
      if (b[i][j]) {
        if (a[j] < mx && !visit[a[j]]) {
          mx = a[j];
          swap(a[i], a[j]);
        }
      }
    }
    visit[mx] = 1;
    ans[i] = mx;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " " << ans[i + 1];
    else
      cout << ans[i + 1];
  }
  return 0;
}
