Maintain two arrays , one for rows operation and one for column operation
int [ ] row = new int [ n ] ; int [ ] col = new int [ n ] ; for ( int i = 0 ; i < size ; i ++ ) { int x = q [ i , 0 ] ; int y = q [ i , 1 ] ;