text: Calculating GCD of each row and each column in O ( mn ) and store in arrays .
code: for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { rgcd [ i ] = gcd ( rgcd [ i ] , mat [ i , j ] ) ; cgcd [ j ] = gcd ( cgcd [ j ] , mat [ i , j ] ) ; } }