I can see that BFS based solutions are able to pass the tests but the DFS based solutions are failing (I am getting stack overflow). Any particular reason as to why BFS should be preferred here ?
In fact, in my test, DFS faster than BFS in this problem. I think you getting stack overflow because you have consider the peripheral border again in DFS, it is unnecessary.
Please get more information from here.