Implement the function remaining_leaves(n, m, leaves, winds) which takes as inputs the integers n and m, the 2D integer array leaves, and the string winds, and returns the number of leaves left on the floor after applying all wind gusts.
The ground is represented by a grid that is n squares high and m squares wide. The top-left box is located at (0, 0), where the first integer represents the row and the second the column. Each element of the grid represents the number of leaves on the ground.
The string winds is composed of the characters U (top), D (bottom), R (right), and L (left).
Each gust of wind moves the leaves on the grid one square in the direction of the wind. Leaves that are pushed out of the grid are lost.
Return the total number of leaves remaining on the grid as an integer.
Example:
winds = "RRD"
After applying the gusts, some leaves may move out of bounds and disappear. The answer is the total count of leaves still inside the grid.
This is a 2D grid simulation problem. Given a matrix of leaves and a sequence of wind directions, you must apply each gust in order, moving every leaf one step and discarding any leaf that leaves the grid. The key is handling the traversal order carefully for each direction so values are not overwritten during the move. A clean implementation usually uses either directional in-place shifts or a fresh grid per gust, then sums the remaining leaves at the end.