Run a loop for finding answer for all queries
for ( int qi = 0 ; qi < q ; qi ++ ) { for ( int i = 0 ; i < n - queries [ qi ] [ 0 ] + 1 ; i ++ ) { for ( int j = 0 ; j < m - queries [ qi ] [ 1 ] + 1 ; j ++ ) { maxSum [ qi ] = max ( maxSum [ qi ] , sumQuery ( dp , i , j , i + queries [ qi ] [ 0 ] - 1 , j + queries [ qi ] [ 1 ] - 1 ) ) ; } } } return maxSum ; }