Simplify the logic and remove some unnecessary code, it is easy to see the time complexity is O(M * N * 4^L).