# Problem Description
- The task involves calculating the number of red squares visible from a given view of a 3D stack of cubes represented by a 2D grid. The red squares appear as the top-most cube among all the cubes. The function accepts a 2D grid cubes where each entry cubes[i][j] specifies the number of stacked cubes at the grid cell (i, j).
- The goal is to implement the function solution, which counts and returns the total number of red squares visible in the specified view.

# 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. Red Squares:
- Red squares represent the top-most cube among all the cubes.
- "Top-most" represents the cube with the largest Z-coordinate.
- Each column (x, y) does not necessarily have a red cube, only the column(s) with the most stacking along the Z-axis can have a red cube.
3. Examples in the image:
- Example 1:
    In the (x, y) view, red squares appear at the highest points (height z).
- Example 2, 3, and 4 follow the same principle.

# Visual Patterns
1. Top-most Cube Identification:
    - A red square is assigned only to the cube(s) with the largest Z-coordinate across the entire 3D grid.
    - This means that not all columns (x, y) will necessarily have a red cube. Only the column(s) with the maximum stacking height along the Z-axis can have red cubes.
2. Single Red Cube per Column:
    - In a column (x, y) where the stacking height matches the maximum Z-coordinate in the grid, there can be one red cube.
3. Columns with No Contribution:
    - Any column (x, y) where the height (stacking along Z-axis) is less than the maximum Z-coordinate does not contribute any red cubes.
4. Global Maximum Z-coordinate:
    - The identification of red cubes depends on determining the global maximum stacking height (largest Z-coordinate) in the entire grid.

# 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.
    """
    max_z = max(max(row) for row in cubes)
    return sum(1 for row in cubes for cell in row if cell >= max_z)


# 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, 1),
        (cubes2, 1),
        (cubes3, 1),
        (cubes4, 1),
        (cubes5, 1),
        (cubes6, 1),
        (cubes7, 3),
        (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()