# Problem Description
This is a matrix transformation problem where elements need to be rearranged according to specific movement rules. The input is a 2D matrix of integers, and the output is a transformed matrix of the same dimensions where elements have been moved according to a cyclical pattern.

# Visual Facts
1. The image shows 4 different example transformations:
   - 2×3 matrix transformation
   - 4×3 matrix transformation
   - 4×4 matrix transformation
   - 3×5 matrix transformation

2. Colors are used to track element movements:
   - Light blue cells
   - Orange cells
   - Green cells

3. All input matrices have consistent color patterns:
   - First row is always light blue
   - first column elements (except first row) are green
   - Remaining elements are orange

4. Output matrices maintain the same dimensions as their inputs

# Visual Patterns
1. Movement Rules:
   - Elements move in a counter-clockwise spiral pattern
   - Top row elements (blue) move to the bottom row
   - First column elements (green) (except first row) move to the rightmost position of the row above
   - Other elements (orange) move one position left and one position above

2. Pattern Consistency:
   - The transformation rules remain consistent regardless of matrix dimensions
   - The transformation preserves all original values, just rearranges them
   - The pattern works for both positive and negative integers


# Solution
def solution(matrix: list[list[int]]) -> list[list[int]]:
    """
    Manipulates a 2D matrix of integers according to the demonstrated transformation rules.

    Parameters:
        matrix (list[list[int]]): A 2D matrix of integers.

    Returns:
        list[list[int]]: The transformed matrix of the same dimensions as the input.       
    """
    # Initialize the row and column sizes
    rows = len(matrix)
    cols = len(matrix[0])

    # Initialize the new matrix
    new_matrix = [[0] * cols for _ in range(rows)]

    # Iterate through each cell in the matrix
    for i in range(rows):
        for j in range(cols):
            # Calculate the sum of the cell and its neighbors
            if i == 0:
                new_matrix[rows-1][j] = matrix[i][j]
            elif j == 0 and i > 0:
                new_matrix[i-1][cols-1] = matrix[i][j]
            else:
                new_matrix[i-1][j-1] = matrix[i][j]
    
    return new_matrix


# Test Script
def test():
    test_cases = [
        ([[6, 5, 8], [1, 8, 2]], [[8, 2, 1], [6, 5, 8]]),
        ([[8, 2, 6], [4, 0, 7], [2, 1, 6]], [[0, 7, 4], [1, 6, 2], [8, 2, 6]]),
        ([[8, 2, 6], [4, 0, 7], [2, 1, 6]], [[0, 7, 4], [1, 6, 2], [8, 2, 6]]),
        ([[8, 1, 5, 9, 1, 4]], [[8, 1, 5, 9, 1, 4]]),
        ([[0, 1, 0], [7, 6, 6], [7, 3, 3], [6, 6, 4], [3, 9, 7], [6, 1, 9]], [[6, 6, 7], [3, 3, 7], [6, 4, 6], [9, 7, 3], [1, 9, 6], [0, 1, 0]]),
        ([[6], [3]], [[3], [6]]),
        ([[4, 9], [2, 4]], [[4, 2], [4, 9]]),
        ([[6, 8, 8, 7], [5, 0, 4, 6], [9, 9, 9, 8], [2, 1, 7, 9], [4, 8, 1, 9], [9, 9, 0, 0]], [[0, 4, 6, 5], [9, 9, 8, 9], [1, 7, 9, 2], [8, 1, 9, 4], [9, 0, 0, 9], [6, 8, 8, 7]]),
        ([[8, 5, 2, 0, 8, 5], [8, 1, 4, 3, 5, 8], [6, 0, 8, 0, 6, 3], [5, 1, 0, 5, 9, 0], [9, 3, 5, 6, 6, 8]], [[1, 4, 3, 5, 8, 8], [0, 8, 0, 6, 3, 6], [1, 0, 5, 9, 0, 5], [3, 5, 6, 6, 8, 9], [8, 5, 2, 0, 8, 5]]),
        ([[6, 9, 1, 3], [5, 8, 9, 8], [5, 2, 7, 9], [8, 0, 9, 6], [9, 3, 4, 4]], [[8, 9, 8, 5], [2, 7, 9, 5], [0, 9, 6, 8], [3, 4, 4, 9], [6, 9, 1, 3]])
    ]

    for matrix, expected_output in test_cases:
        try:
            output = solution(matrix)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`matrix`: {matrix}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n" 
                f"`matrix`: {matrix}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()