int i, j, data[M][N], total[(M/p)*(N/q)];

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