def is_stochastic_matrix(m, ep=1e-08) ->bool:
    """Checks that the matrix m (a list of lists) is a stochastic matrix."""
    for i in range(len(m)):
        for j in range(len(m[i])):
            if m[i][j] < 0 or m[i][j] > 1:
                return False
        s = sum(m[i])
        if abs(1.0 - s) > ep:
            return False
    return True
