First, start breaking up the problem into smaller pieces first. I defined a basic sum function that is defined using the even more basic add function. map ping sum over the input array will give you horizontal sums.
Vertical amounts are a little trickier, but not too tough. I defined a transpose function that rotates our matrix. Once we rotate, we can sum lines in the same way.
This solution works on any MxN matrix.
source share