# Problem Description
This problem involves counting the number of vertical edges in a given matrix. An edge is defined as a connection between two vertically adjacent cells that contain the same letter. The goal is to return the total count of such vertical edges in the given matrix.

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

2. **Edge Representation**:
   - Edges are depicted as red lines connecting vertically adjacent cells.
   - Edges only connect cells with the same letter.
   - The first matrix has 7 edges.
   - The second matrix has 12 edges.

3. **Example Counts**:
   - The first 4×4 matrix results in 7 vertical edges.
   - The second 5×6 matrix results in 12 vertical edges.

# Visual Patterns
1. **Edge Formation Rules**:
   - An edge forms only between two vertically adjacent cells containing the same letter.
   - Horizontal adjacencies are not considered for edge formation in this problem.
   - Each edge connects exactly two cells.

2. **Counting Patterns**:
   - For each cell in the matrix, check the cell directly below it.
   - If the cell below contains the same letter, count it as an edge.
   - Continue this process for all cells except those in the last row (as they have no cell below them).

3. **Calculation Method**:
   - Iterate through each cell in the matrix.
   - For each cell, compare it with the cell directly below it.
   - If they contain the same letter, increment the edge count.
   - Sum all valid connections to get the total number of vertical 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 - 1):
        for j in range(cols):
            if input_matrix[i][j] == input_matrix[i + 1][j]:
                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': 9
        },
        {
            'matrix': [
                ['A', 'B', 'C', 'D'],
                ['A', 'B', 'C', 'D'],
                ['D', 'B', 'C', 'D'],
                ['D', 'B', 'C', 'D']
            ],
            'expected': 11
        },
        {
            'matrix': [
                ['A', 'B', 'B', 'D'],
                ['A', 'B', 'B', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D']
            ],
            'expected': 9
        },
        {
            'matrix': [
                ['A', 'B', 'B', 'D'],
                ['A', 'B', 'B', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D']
            ],
            'expected': 13
        }, 
        {
            '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': 15
        },
        {
            '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': 14
        }
    ]
    for test_case in test_cases:
        input_matrix = test_case['matrix']
        expected_output = test_case['expected']
        try:
            output = solution(input_matrix)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`input_matrix`:\n"
            )
            for row in input_matrix:
                error_message += f"{row}\n"
            error_message += f"\nException: '{str(e)}'"
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`input_matrix`:\n"
            )
            for row in input_matrix:
                error_message += f"{row}\n"
            error_message += (
                f"\nActual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()