trailing_zeros to store the count of trailing zeros for each row.i from 0 to n-1, count the number of trailing zeros and store it in trailing_zeros[i].steps to 0, which will count the minimum number of swaps needed.i from 0 to n-1, do the following:
a. Calculate the required number of trailing zeros for the current row, which is n - i - 1.
b. Find the nearest row below the current one that has at least the required number of trailing zeros.
c. If such a row is found, increment steps by the distance between the current row and the found row, and perform the swaps by updating the trailing_zeros array.
d. If no such row is found, return -1 as it is impossible to make the grid valid.steps as the minimum number of swaps needed.