solve matrix-pattern