Compute block sums for every position in a matrix with a given radius.
You are given a 2D list `mat` (list of lists of integers) with dimensions `m x n`, and an integer `k`. For each cell `(i, j)`, define the block centered at that cell with radius `k`. The block includes all cells `(r, c)` such that `max(|r - i|, |c - j|) <= k`. Cells outside the matrix boundaries are treated as 0. Your task is to return a new matrix `answer` of the same dimensions where `answer[i][j]` is the sum of all cells in the block centered at `(i, j)`.
Implement the function `matrix_block_sum(mat, k)` that returns the resulting matrix as a list of lists of integers.
Constraints
- 1 <= m, n <= 200
- 0 <= k <= 1000
- -1000 <= mat[i][j] <= 1000
- The input matrix is always non-empty.
- The sum of any block fits within a 64-bit signed integer.
Example
>>> matrix_block_sum([[1,2,3],[4,5,6],[7,8,9]], 1)
[[12, 21, 16], [27, 45, 33], [24, 39, 28]]
>>> matrix_block_sum([[1,2,3],[4,5,6],[7,8,9]], 0)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> matrix_block_sum([[1,2],[3,4]], 2)
[[10, 10], [10, 10]]
25 points
~25 min