recursive solution works fine. But you shoud now that it will fail with stackoverflow error for very big maps(10^3 x 10^3 )