#include <bits/stdc++.h>
using namespace std;
int num, a[305], b, x;
string s[305];
bool vis[304], u[305];
inline void dfs(int i) {
  if (!u[a[i]]) x = min(x, a[i]);
  vis[i] = true;
  for (int j = 0; j < num; j++)
    if (s[i][j] == '1' && !vis[j]) dfs(j);
}
int main() {
  cin >> num;
  for (int i = 0; i < num; i++) cin >> a[i];
  for (int i = 0; i < num; i++) cin >> s[i];
  for (int i = 0; i < num; i++) {
    x = num + 1;
    memset(vis, false, sizeof(vis));
    dfs(i);
    u[x] = true;
    cout << x << " ";
  }
  return 0;
}
