Base case
dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] [ 0 ] = ( mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ) NEW_LINE DEDENT for j in range ( 1 , M ) : NEW_LINE INDENT dp [ 0 ] [ j ] = ( mat [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] ) NEW_LINE DEDENT