DFS is good choice but it fails with big input maps(10^3 x 10^3). Recursion consumes stack to store entry points. You can dynamically allocate your own stack container. 

P.S. It's not an error just suggestion for the future solutions.