You need to check it by row, and keep a count of each number that you find. So create a second array that is as long as the highest number input + 1 (to allow for zeroes). Then as you traverse each row, for each number that you find use it as an index into the array, and add 1 to the relevant cell. So for your matrix above you would end up with something like:

C++

0, 1, 2, 3, 2, 1// i.e no zeroes, 1 x 1, 2 x 2, 3 x 3, 2 x 4, 1 x 5.