Denote by X the set of all matrices with entries in the set {1,2,3,...,p} and let Ai, respectively Bj, the set of matrices in which the sum of elements in the row i, respectively on the column j, be divisible by p. Then we have to obtain the cardinality N of the set
X∖((i=1⋃mAi)∪(j=1⋃nBj)).
For this we apply the inclusion-exclusion principle. Then
N=pmn+i=0∑mj=0∑n(−1)i+jk1<k2<⋯<kil1<l2<⋯<lj∑∣Ak1∩Ak2∩⋯∩Aki∩Bl1∩Bl2∩⋯∩Blj∣(i)
the sum being taken for all i,j with i+j=0.
But we have
∣Ak1∩Ak2∩⋯∩Aki∩Bl1∩Bl2∩⋯∩Blj∣=∣A1∩A2∩⋯∩Ai∩B1∩B2∩⋯∩Bj∣(ii)
and moreover
pmn=(−1)0+0(0m)(0n)pmn−0−0.
For all i,j participating in the summation we have
∣A1∩A2∩⋯∩Ai∩B1∩B2∩⋯∩Bj∣={pmn−i−j,pmn−m−n−1,if i=m or j=nif i=m and j=n
So,
N=i=0∑mj=0∑mi+j=m+n∑(−1)i+j(im)(jn)pmn−i−j+(−1)m+n(mm)(nn)pmn−m−n+1==i=0∑mj=0∑m(−1)i+j(im)(jn)pmn−i−j−(−1)m+n(mm)(nn)pmn−m−n++(−1)m+n(mm)(nn)pmn−m−n+1=
---
=s=0∑m+n(−1)spmn−si+j=s∑(im)(jn)+(−1)m+n(mm)(nn)pmn−m−n(p−1)==pmns=0∑m+n(p−1)s(sm+n)+(−1)m+npmn−m−n(p−1)==pmn((−p1)m+n+(−1)m+n(mm)(nn)pmn−m−n(p−1))==pmn−m−n((p−1)m+n+(−1)m+n(p−1))