Try to use hints only when you really need help! The hints are arranged in order.
This can be achieved using a Flood fill algorithm, but you must add your own custom checks regarding the depth level, and change the notion of "same colour" to "obstacle".
Encountering a timeout on your code for more complex cases? Perhaps consider how efficient your current algorithm is. Does it constantly excessively waste time computing something that would be overwritten later? Is there a way to minimize this wastage in computational time?