# Problem Description
The task involves analyzing a 3D stack of cubes and determining how many "front-most cubes" are visible when viewing the stack from the front side. The function `solution` accepts a 2D list `cubes` where `cubes[i][j]` represents the number of cubes stacked at coordinate `(i, j)` on a 2D grid. The goal is to count and return the total number of "front-most cubes" visible from the front side.

# Visual Facts
1. **3D Grid Representation**:
   - The 3D structure is built on a 2D coordinate plane defined by the x and y axes, with the z axis representing the height (stacked cubes).
   - Each (x, y) cell contains a number of stacked cubes.
   
2. **Front-most Cubes**:
   - Front-most cubes are highlighted in red in the image.
   - These cubes are the first cubes encountered when looking from the front side along the y-axis.
   
3. **Examples in the Image**:
   - **Example 1**: 
     - The stack heights are as follows: (0, 0) has a height of 2, (0, 1) has a height of 1, (0, 2) has a height of 3, (1, 0) has a height of 1, and (1, 2) has a height of 1.
     - The front-most cubes are the cubes stacked on the front-most stacks, which are the stacks with the minimum y-coordinate, located at (0, 0), (1, 0).
     - The number of red cubes is 2 + 1 = 3.
   - **Example 2**: 
     - The stack heights are as follows: (0, 0) has a height of 2, (0, 1) has a height of 1, (0, 2) has a height of 1, and (1, 2) has a height of 1.
     - The front-most cubes are the cubes stacked on the front-most stacks, which are the stacks with the minimum y-coordinate, located at (0, 0).
     - The number of red cubes is 2.
   - **Example 3**: 
     - The stack heights are as follows: (0, 0) has a height of 2, (0, 1) has a height of 1, (0, 2) has a height of 2, (1, 0) has a height of 1, (1, 2) has a height of 1, and (2, 2) has a height of 1.
     - The front-most cubes are the cubes stacked on the front-most stacks, which are the stacks with the minimum y-coordinate, located at (0, 0), (1, 0).
     - The number of red cubes is 2 + 1 = 3.
   - **Example 4**: 
     - The stack heights are as follows: (0, 0) has a height of 1, (0, 1) has a height of 1, (0, 2) has a height of 1, (1, 2) has a height of 1, and (2, 2) has a height of 1.
     - The front-most cubes are the cubes stacked on the front-most stacks, which are the stacks with the minimum y-coordinate, located at (0, 0).
     - The number of red cubes is 1.

# Visual Patterns
1. **Front-most Cube Identification**:
   - A cube is considered a "front-most cube" if it is the first cube encountered when looking from the front in its column (along the y-axis).
   - - The forward direction of x in the picture represents the direction of the increase in the number of rows of the matrix, and the forward direction of y represents the direction of the increase in the number of columns of the input matrix, which means that the frontmost direction in the picture corresponds to the first **non-zero** column of the input matrix

2. **Counting Front-most Cubes**:
   - The total number of cubes with the front-most y-coordinate(the first non-zero column) is the desired answer.

# Solution
from typing import List

def solution(cubes: List[List[int]]) -> int:
    """
    Count the red squares from a specified view of the stacked cubes.

    Parameters:
        cubes (List[List[int]]): A 2D list where cubes[i][j] represents the number of cubes stacked at coordinate (i, j). (0-indexed)

    Returns:
        int: The number of red squares from the given view.
    """
    # for each row, find the first non-zero element in the first column
    first_none_zero_column = len(cubes[0])
    for i in range(len(cubes)):
        for j in range(0, len(cubes[0])):
            if cubes[i][j] != 0:
                first_none_zero_column = min(first_none_zero_column, j)
                break
    red_squares = sum(cubes[i][first_none_zero_column] for i in range(len(cubes)))
    return red_squares


# Test Script
def test():
    cubes1 = [[2, 1, 3], [1, 2, 1]] 
    cubes2 = [[2, 1, 3], [1, 0, 1]]
    cubes3 = [[2, 1, 3], [1, 1, 1]]
    cubes4 = [[0, 2], [0, 4]]
    cubes5 = [[1, 2], [3, 0], [5, 6]]
    cubes6 = [[1, 2], [3, 0], [5, 6], [0, 8]]
    cubes7 = [[1, 2], [3, 4], [5, 6], [7, 9], [9, 9]]
    cubes8 = [[3, 1],  [3], [2, 2], [1]]
    test_cases = [
        (cubes1, 3),
        (cubes2, 3),
        (cubes3, 3),
        (cubes4, 6),
        (cubes5, 9),
        (cubes6, 9),
        (cubes7, 25),
        (cubes8, 9)
    ]

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

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

test()