# Problem Description
This is a grid-based edge counting problem where:
- We need to count the number of edges between adjacent cells containing identical letters
- An edge exists when two adjacent cells (horizontally or vertically) contain the same letter
- The goal is to return the total count of such edges in the given matrix

# Visual Facts
1. Matrix Structure:
   - Two example cases are shown
   - First case: 4×4 matrix with 12 edges
   - Second case: 5×6 matrix with 22 edges
   - Matrices contain letters A, B, C, and D

2. Edge Representation:
   - Edges are shown as red lines connecting adjacent cells
   - Edges only connect cells with matching letters
   - Edges can be horizontal or vertical
   - Each edge connects exactly two cells

3. Example Counts:
   - First matrix results in 12 edges
   - Second matrix results in 22 edges

# Visual Patterns
1. Edge Formation Rules:
   - An edge forms only between two adjacent cells containing identical letters
   - Adjacency is only vertical or horizontal (not diagonal)
   - Multiple edges can connect to the same cell (up to 4 possible edges per cell)

2. Counting Patterns:
   - Each edge is counted exactly once
   - The same letter can form multiple separate groups of connections
   - Letter groups can form different shapes (squares, lines, etc.)

3. Calculation Method:
   - Need to check both horizontal and vertical adjacencies
   - For horizontal edges: compare each cell with its right neighbor
   - For vertical edges: compare each cell with its bottom neighbor
   - Sum all valid connections to get total 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
    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': 12
        },
        {
            'matrix': [
                ['A', 'A', 'C', 'D'],
                ['A', 'A', 'C', 'D'],
                ['D', 'B', 'C', 'D'],
                ['D', 'B', 'C', 'C']
            ],
            'expected': 12
        },
        {
            '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': 13
        },
        {
            'matrix': [
                ['A', 'B', 'B', 'D'],
                ['A', 'B', 'B', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D'],
                ['D', 'C', 'C', 'D']
            ],
            'expected': 18
        }, 
        {
            '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': 20
        },
        {
            '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': 18
        }
    ]

    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"
            )
            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"
            )
            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()