Generalized exact formula for the correlations of the Ising model and other classical systems