C # implementation of the approach
using System ; class GFG { static int MAXI = 50 ; static int [ , , ] dp = new int [ MAXI , MAXI , MAXI * MAXI ] ; static int n , m ; static int INT_MAX = int . MaxValue ;