Calculate how many submatrices of a given size exist in a matrix

I want to calculate how many square submatrices of size k (fixed) exist within a matrix of size N x M. Submatrices can be overlapping. I think this can be simplified to a formula in terms of N,M and k, but my solution is incorrect.