Reshape the Matrix

class Solution:
    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
        M = len(mat)
        N = len(mat[0])
        
        if (M * N) != (r * c):
            return mat

        out = [[0] * c for _ in range(r)]
        
        for ix in range(M * N):
            out[ix // c][ix % c] = mat[ix // N][ix % N]
        
        return out