Ranks of matrices with few distinct entries