If a single row is present
for i in range ( 1 , min ( m , X ) ) : NEW_LINE INDENT dp [ 0 ] [ i ] = ( dp [ 0 ] [ i - 1 ] + grid [ 0 ] [ i - 1 ] ) NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 1 , X + 1 ) : NEW_LINE DEDENT