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:
0, 1, 2, 3, 2, 1