Knight Moves
InterviewTime limit1sMemory limit128 MB
Find the fewest knight moves from K to X on a grid with blocked squares, or output -1 when unreachable.
Problem
In chess a knight moves two squares in one direction and then one square in a perpendicular direction. A knight jumps, so only the destination square has to be free; the squares it passes over may be occupied. In the diagram below the knight can move to any of the squares marked X.

You are given a grid, a starting square and a destination square. Find the least number of moves the knight needs to get from the start to the destination. Some squares of the grid may be blocked, and the knight cannot move onto them.
Input
The input consists of a single test case. The first line contains two integers and (), the height and the width of the grid. Each of the next lines contains characters describing the grid. The grid contains only . (an open square), # (a blocked square), K (the square the knight starts on) and X (the square the knight has to reach). There is exactly one K and exactly one X.
Output
Print a single line with one integer, the minimum number of moves the knight needs to reach the destination, or -1 if it cannot reach it. Print no spaces.