Optimal extraction of motif patterns in 2D