boundary_elements(grid) for a rectangular 2D list grid with at least one row and column. Return a flat list of all elements on the outer boundary (first row, last row, first column, last column) in clockwise order starting from the top-left corner, without repeating any corner element.grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[1, 2, 3, 6, 9, 8, 7, 4]
Top row left-to-right, right column top-to-bottom (excluding corner already used), bottom row right-to-left, left column bottom-to-top.
If there is only one row or one column, handle it as a special case to avoid duplicating corners.
Walk top row, then right column (skip first row), then bottom row reversed (skip last column), then left column reversed (skip first and last row).