Create a memoization table and initialize all entries as - 1
var mem = Array ( R ) . fill ( ) . map ( ( ) => Array ( C ) . fill ( ) . map ( ( ) => Array ( C ) . fill ( 0 ) ) ) ; for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { for ( l = 0 ; l < C ; l ++ ) mem [ i ] [ j ] [ l ] = - 1 ; } }