===== ISTEM =====
Solve the following programming task.

TASK: Rotate a 2D Matrix 90 Degrees Clockwise

Write a function 'rotate_matrix(matrix)' that rotates an N x N two-dimensional matrix by 90 degrees clockwise in place.

STARTER CODE (python):
def rotate_matrix(matrix: list[list[int]]) -> list[list[int]]:
    # Write your code here
    pass


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```python
def rotate_matrix(matrix: list[list[int]]) -> list[list[int]]:
    n = len(matrix)
    for layer in range(n // 2):
        first, last = layer, n - 1 - layer
        for i in range(first, last):
            offset = i - first
            top = matrix[first][i]  # save top

            # left -> top
            matrix[first][i] = matrix[last - offset][first]

            # bottom -> left
            matrix[last - offset][first] = matrix[last][last - offset]

            # right -> bottom
            matrix[last][last - offset] = matrix[i][last]

            # top -> right
            matrix[i][last] = top
    return matrix
```