Compute sum of all elements , sum of every row and sum every column
for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { sum += mat [ i ] [ j ] ; col [ j ] += mat [ i ] [ j ] ; row [ i ] += mat [ i ] [ j ] ; } }