The massive scalar field with $\lambda\varphi^4$ interaction placed in $(3+1)$ dimensional box is considered. The sizes of the box are $V\times \beta$ $(V=L^3$ is the volume, $T=1/\beta$ is the temperature). The free energy is evaluated up to the 2nd order of $PT$. The averaging on the vacuum fluctuations is separated from the averaging on the thermal fluctuations explicitly. As result the free-energy is expressed through the scattering amplitudes. We find that in 3-loop approximation the expression for free energy coincides with the ansatz of Bernstein, Dashen, Ma suggested on the base of $S$-matrix formulation of statistical mechanics. The obtained expressions are generalized for higher order of $PT$.