recursive solution which works but is too slow for the 20x20 case