# Problem Description
The task involves analyzing a 3D stack of cubes and determining how many "rightmost cubes" are visible when viewing the stack from the right side. The function `solution` accepts a 2D list `cubes` where `cubes[i][j]` represents the number of cubes stacked vertically at coordinate `(i, j)` on a 2D grid. The goal is to count and return the total number of "rightmost cubes" visible from the right 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. **Rightmost Cubes**:
   - Rightmost cubes are highlighted in red in the image.
   - These cubes are the first cubes encountered when looking from the right side along the x-axis.
   
3. **Examples in the Image**:
   - **Example 1**: 
     - In the figure, 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 rightmost cubes are the cubes stacked on the rightmost stacks, which are the stacks with the maximum x-coordinate, located at (1, 0) and (1, 2).
     - The number of red cubes is the sum of the hight of rightmost stacks that is 1 + 1 = 2.
   - **Example 4**: 
      - In the figure, 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 rightmost cubes are the cubes stacked on the rightmost stacks, which are the stacks with the maximum x-coordinate, located at (2, 2).
      - The number of red cubes is 1.

# Visual Patterns
1. **Rightmost Cube Identification**:
   - A cube is considered a "rightmost cube" if it is the first cube encountered when looking from the right in its row (along the x-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 rightmost direction in the picture corresponds to the last **non-zero** row of the input matrix
   
2. **Counting Rightmost Cubes**:
      - The total number of cubes with the rightmost x-coordinate(the last **non-zero** row) 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 column, find the first non-zero element in the last row
    last_none_zero_row = 0
    for j in range(len(cubes[0])):
        for i in range(len(cubes)-1, -1, -1):
            if cubes[i][j] != 0:
                last_none_zero_row = max(last_none_zero_row, i)
                break
    red_squares = sum(cubes[last_none_zero_row])
    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 = [[1, 2], [3, 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 = [[0, 0], [0, 0], [0, 0], [0, 1], [0, 0]]
    test_cases = [
        (cubes1, 4),
        (cubes2, 2),
        (cubes3, 3),
        (cubes4, 7),
        (cubes5, 11),
        (cubes6, 8),
        (cubes7, 18),
        (cubes8, 1)
    ]
    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()