Traverse through matrix and find pairs p1 and p2
for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr . get ( i ) . charAt ( j ) == '1' ) if ( p1 . first == - 1 ) { p1 . first = i ; p1 . second = j ; } else { p2 . first = i ; p2 . second = j ; } } } p3 = p1 ; p4 = p2 ;