# Problem Description
This problem involves counting the number of edges in a grid where each cell contains a letter (A, B, C, or D). An edge is defined as a connection between two horizontally adjacent cells that contain the same letter. The goal is to return the total number of such edges in the given matrix.

# Visual Facts
1. **Matrix Structure**:
   - Two example matrices are shown.
   - The first matrix is a 4x4 grid.
   - The second matrix is a 5x6 grid.
   - Each cell in the matrices contains one of the letters A, B, C, or D.

2. **Edge Representation**:
   - Edges are represented as red lines connecting horizontally adjacent cells.
   - Only horizontally adjacent cells with the same letter are connected by an edge.
   - The number of edges is explicitly counted and displayed for each matrix.

3. **Example Counts**:
   - The first matrix has 4 edges.
   - The second matrix has 10 edges.

# Visual Patterns
1. **Edge Formation Rules**:
   - An edge is formed only between two horizontally adjacent cells containing the same letter.
   - Vertical adjacency does not count towards the edge count.
   - Each edge connects exactly two cells.

2. **Counting Patterns**:
   - For each row in the matrix, compare each cell with its right neighbor.
   - If the letters in both cells are the same, an edge is counted.
   - The total number of edges is the sum of all such valid horizontal connections.

3. **Calculation Method**:
   - Iterate through each row of the matrix.
   - For each cell in a row, compare it with the cell to its immediate right.
   - If the letters match, increment the edge count.
   - Continue this process for all rows to get the total number of edges.

# Solution
def solution(input_matrix: list[list[str]]) -> int:
    """
    Count the number of edges dirived from the given matrix.

    Args:
        input_matrix (list[list[str]]): A 2D list where input_matrix[i][j] represents a character at row i, column j.

    Returns:
        int: The number of edges in the given matrix.
    """
    rows = len(input_matrix)
    cols = len(input_matrix[0])
    edges = 0
    for i in range(rows):
        for j in range(cols - 1):
            if input_matrix[i][j] == input_matrix[i][j + 1]:
                edges += 1
    return edges


# Test Script
def test():
    test_cases = [
        {
            'matrix': [
                ['A', 'B', 'C'],
                ['D', 'E', 'F'],
                ['G', 'H', 'I']
            ],
            "expected": 0
        },
        {
            'matrix': [
                ['A', 'A', 'A'],
                ['A', 'A', 'A'],
                ['A', 'A', 'A']
            ],
            'expected': 6
        },
        {
            'matrix': [
                ['A', 'A', 'C', 'D'],
                ['A', 'A', 'C', 'D'],
                ['D', 'B', 'C', 'D'],
                ['D', 'B', 'C', 'C']
            ],
            'expected': 3
        },
        {
            'matrix': [
                ['A', 'B', 'C', 'D'],
                ['A', 'B', 'C', 'D'],
                ['D', 'B', 'C', 'D'],
                ['D', 'B', 'C', 'D']
            ],
            'expected': 0
        },
        {
            'matrix': [
                ['A', 'B', 'B', 'D'],
                ['A', 'B', 'B', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D']
            ],
            'expected': 4
        },
        {
            'matrix': [
                ['A', 'B', 'B', 'D'],
                ['A', 'B', 'B', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D']
            ],
            'expected': 5
        },
        {
            'matrix': [
                ['D', 'A', 'B', 'C', 'D'],
                ['A', 'A', 'B', 'C', 'D'],
                ['D', 'C', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'A', 'D'],
                ['D', 'C', 'C', 'A', 'D']
            ],
            'expected': 5
        },
        {
            'matrix': [
                ['A', 'B', 'C', 'D', 'E'],
                ['A', 'B', 'C', 'D', 'E'],
                ['D', 'D', 'C', 'D', 'E'],
                ['D', 'C', 'C', 'C', 'E'],
                ['D', 'B', 'C', 'E', 'E']
            ],
            'expected': 4
        }
    ]
    for test_case in test_cases:
        try:
            output = solution(test_case['matrix'])
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`input_matrix`:\n"
            )
            # Format the matrix for better readability
            for row in test_case['matrix']:
                error_message += f"{row}\n"
            error_message += f"\nException: '{str(e)}'"
            assert False, error_message

        if output != test_case['expected']:
            error_message = (
                f"The following Test Case failed:\n"
                f"`input_matrix`:\n"
            )
            # Format the matrix for better readability
            for row in test_case['matrix']:
                error_message += f"{row}\n"
            error_message += (
                f"\nActual Output: {output}\n"
                f"Expected Output: {test_case['expected']}"
            )
            assert False, error_message

test()