Swap any bug with its vertical or horizontal neighbour to form a row of 3…