if np.any(np.mod(x.shape, np.array(submat_shape)) != 0):
    raise Exception('Input array shape is not divisible by submatrix shape!')
if order == 'C':
    x4D = x.reshape(-1, p, n / q, q).transpose(0, 2, 1, 3).reshape(-1, p, q)
How to get all submatrices of given size in numpy?