We developed a 51-channel magnetocardiography (MCG) system based on weakly damped dc superconducting quantum interference device (SQUID) magnetometers. In previous works, the system consisted of 36 sensing magnetometers, 12 reference magnetometers, and a three-component vector magnetometer. In this paper, we have describe the optimizing processes of software gradiometer configurations in different magnetically shielded rooms (MSRs). Using a simulated MCG signal generated by a small coil driven by electrocardiograph, we have quantitatively evaluated the signal quality through four key performance indices, which are 1) correlation in time domain, 2) correlation in frequency domain, 3) relative heights of different peaks, and 4) magnetic field map of simulating coil. After data processing and analysis of these four parameters, optimized software gradiometers of first or second order can be formed by using specific reference magnetometer signals to compensate the signals of 36 sensing magnetometers in each MSR. Also, the MCG signals of an adult have been detected by using these optimized configurations. The magnetic field maps and typical parameters at T-peak have been extracted and compared, and the results with high consistency confirm the effectiveness of optimization processes.