Suppose the vectors and columns of sums are $\underline{r}$ and $\underline{c}$ respectively. And suppose the number of solutions is $S(\underline{r},\underline{c})$
Suppose the sum in the first row is $r_{1}$ and the first column is $c_{1}$. Let $m_{1,1} = \operatorname{min}(r_{1},c_{1})$. Then the element $a_{1,1}$ has the range ${0,1,...,m_{1,1}}$
\begin{equation}
S(\underline{r},\underline{c}) = \sum\limits_{k=0}^{m_{1,1}} S(\underline{r'},\underline{c'})
\end{equation}
Where $r'$ and $c'$ are the appropriately modified vectors. After having derived this formula. We can go backwards,…