Get the pair from the top and from left
pair top = dp [ i - 1 ] [ j ] ; pair left = dp [ i ] [ j - 1 ] ; pair curr = new pair ( countTwos ( mat [ i ] [ j ] ) , countFives ( mat [ i ] [ j ] ) ) ; top = add ( top , curr ) ; left = add ( left , curr ) ;