Rotating the Box
We can solve this problem by performing two steps:
Handle the Gravity: Simulate the falling of stones within each row. Iterate through each row from left to right and keep track of the number of stones and their rightmost position relative to obstacles. When an obstacle is found, fill the rightmost available positions with stones and clear the original positions.
Rotate the Box: After simulating gravity, create a new 2D array with dimensions switched (i.e., n x m) and fill it with the rotated values of the original array.
Here’s the code:


Explanation:
 In the first loop, we iterate through each row and handle the gravity within the row.
 In the second loop, we create a new 2D array and fill it with the values from the original array, rotated 90 degrees clockwise.
This solution handles the gravity within each row independently and then constructs the final rotated array, satisfying the given constraints and requirements.