int data[M*N], total[q*p], n, t, i;

for(i=0; i < M*N; i++)
total[i/(M/p)%(M/p) + (i/M)/(N/q)*(M/p)] += data[i];