Implement a simplified version of a specific attention layer or model component from scratch. Implement scaled dot-product attention using pure Python lists, stable softmax computation, and an optional boolean attention mask. The function accepts query, key, and value matrices shaped [sequence_length, dimension], plus an optional mask shaped [query_length, key_length], and returns an output matrix shaped [query_length, value_dimension].
def scaled_dot_product_attention(query, key, value, mask=None):