Finding GCD of each row and column and replacing with each element with maximum of GCD of row or column .
static void replacematrix ( int [ ] [ ] mat , int n , int m ) { int [ ] rgcd = new int [ R ] ; int [ ] cgcd = new int [ C ] ;