matrix   horizontal scan   l-r diagonal   vertical scan   r-l diagonal   sums of all 4 scans by pixels
A1 B1   A1+B1 A1+B1   A1+B2 B1   A1+A2 B1+B2   A1 B1+A2   4A1+B1+A2+B2 4B1+A1+A2+B2
A2 B2   A2+B2 A2+B2   A2 A1+B2   A1+A2     B1+A2 B2   4A2+A1+B1+B2 4B2+A1+B1+A2

Adding all 4 ray sums together:
=7(A1+A2+B1+B2)
But (A1+A2+B1+B2)= sum of original matrix.

Solving for n & bg
row1
(1) ((4A1+B1+A2+B2 - bg) + (4B1+A1+A2+B2 - bg))/n = A1+B1
row2
(2) ((4A2+A1+B1+B2 - bg) + (4B2+A1+B1+A2 - bg))/n = A2+B2
subtracting:
3A1+3B1-3A2-3B2 = (A1+B1-A2-B2)n
hence n=3

substituting n into equation (1) above:
bg = A1+A2+B1+B2
A1+A2+B1+B2 = 1/7 of the sum of the matrix sum
therefore
bg = (sum of ray sums)/7