The Degree of Freedom of the Pearson's X^2-test

everyone.

The degree of freedom for a Person's X^2 test is (I-1)*(J-1) and it is obtained by (IJ-1) -([I-1]+[J-1]) =(I-1)(J-1).

I am just wondering why it is (IJ-1) -([I-1]+[J-1])??

What is (IJ-1) referred to and what is [I-1]+[J-1] referred to and why do we use the difference of those two terms?

I go through some online material, but most of them just directly state that the df is (I-1)(J-1) without process.

My personal thought is (IJ-1) is refers to the situation when the X Y variable is independent and (I-1)(J-1)is referred to the situation when they are not independent.

I am just quite curious about the process and I am not very familiar with X^2 distribution and degree of freedom.

Thank you. : )

The heuristic answer is that there are $$IJ$$ numbers in the table, but since they add to the sample size, only $$IJ-1$$ degrees of freedom. The test estimates the row and column margins in order to compute expected proportions, and this uses up $$I-1$$ and $$J-1$$ degrees of freedom respectively, so there are $$(IJ-1)-(I-1)-(J-1)$$ left.