Optimized FairyChess. Now passes all the test cases. O(T*M*N^2)