#include <bits/stdc++.h>
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-8;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int mp[MAXN][MAXN];
int pos[MAXN], val[MAXN], n;
void Floyd() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (mp[i][j] == 0) mp[i][j] = mp[i][k] & mp[k][j];
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    int tmp;
    scanf("%d%*c", &tmp);
    pos[tmp] = i, val[i] = tmp;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      char tmp;
      scanf("%c", &tmp);
      mp[i][j] = tmp - '0';
    }
    getchar();
  }
  Floyd();
  for (i = 1; i <= n; i++)
    for (j = 1; j < val[i]; j++)
      if (pos[j] > i) {
        if (mp[i][pos[j]]) {
          int oriPos = pos[j];
          val[oriPos] = val[i];
          pos[val[i]] = oriPos;
          val[i] = j;
          pos[j] = i;
          break;
        }
      }
  for (i = 1; i <= n; i++) {
    if (i == 1)
      printf("%d", val[i]);
    else
      printf(" %d", val[i]);
  }
  puts("");
  return 0;
}
