You're not quite clear on how what you want to do depends on the actual contents of M -- I'm guessing this contains some sort of variable count you want to add to your individual counts?
In this case, how about something like this:
int j, i, ref;
int counts[5];
for(ref=0; ref<5; ref++)
counts[ref]=0;
for(j=0; j<4; j++)
for(i=0; i<5; i++)
for(ref=0; ref<5; ref++)
counts[ref]+=M[j][i][0][ref][0];
(I have replaced your individual "count" variables with an array, which makes things much easier.)
Edit: I just saw that you don't care what the contents of M are. In that case, I don't really understand what you're trying to do. Since the dimensions are constant, the number of entries that exist for a certain value of ref
are always constant, and are always the same for all ref
-- in this case, 5*5=25, since you have five entries along each of the j and i dimensions.
If this is not what you want, please clarify.