The purpose of this paper finds an optimal disturbed square matrix for steganography. The method is inspired from square matrix encoding (SME). SME obtains stego images by embedding secret data into cover images through the square matrix. To improve security and image quality, dynamic programming strategy is applied to disturb the digits in the square matrix on the basis of a given cover image and secret data. Through the disturbed square matrix, the stego images with the best image quality can be obtained. Moreover, the embedded data can not be extracted without the corresponding disturbed square matrix. Therefore, the security of the embedding mechanism can be improved. The experimental results show that the proposed method is superior to the other related methods.