Initialize the first row and column explicitly
for ( int i = 1 ; i < n ; i ++ ) dp [ i , 0 ] = add ( dp [ i - 1 , 0 ] , new pair ( countTwos ( mat [ i , 0 ] ) , countFives ( mat [ i , 0 ] ) ) ) ; for ( int i = 1 ; i < m ; i ++ ) dp [ 0 , i ] = add ( dp [ 0 , i - 1 ] , new pair ( countTwos ( mat [ 0 , i ] ) , countFives ( mat [ 0 , i ] ) ) ) ;