Java implementation of the approach
class GFG { final static int MAXI = 50 ; static int dp [ ] [ ] [ ] = new int [ MAXI ] [ MAXI ] [ MAXI * MAXI ] ; static int n , m ; final static int INT_MAX = Integer . MAX_VALUE ;