Solution of the congruence problem for arbitrary hermitian and skew-hermitian matrices over polynomial rings