True, a N*M matrix can be neatly divided into d*d submatrices only if both N and M are divisible by d. Furthermore, there are up to three types of non-d*d submatrices:
(N%d)*d, d*(M%d), and the corner's (N%d)*(M%d).
You must have a reason to do the summation and that reason should guide your decision about what to do with the "leftovers".
* You could simply omit the leftovers, but then some large values will not contribute to the sum.
* You could include maxes of those smaller matrices, increasing the overall sum.
* Something else.