As lithography still pushing toward to low-k1 region, resolution enhancement techniques (RETs) including source optimization (SO) and mask optimization (MO) are expected to overcome the fundamentally physics in optics. Recently inverse lithography (IL) is widely studied for source and mask optimization (SMO) to enhance the resolution for over diffraction limit integrate circuit (IC) patterns. In this paper, we propose a gradient based SMO algorithm where the SO and MO are two sequential steps due to their different image formation mechanism. Moreover, we employ three cost functions including aerial and resist image and the image contrast which is proposed in our previous work. We show that IL patterns produced by SMO have better pattern fidelity and image contrast than MO only patterns.