Buggy code:
inQueue [ ( first - 1 ) ] [ ( second - 1 ) ] = true ; } if ( ( ( ( ( first - 1 ) >= 0 ) && ( ( visited [ ( first - 1 ) ] [ second ] ) == false ) ) && ( ( visitedThisTime [ ( first - 1 ) ] [ second ] ) == false ) ) && ( ( inQueue [ ( first - 1 ) ] [ second ] ) == false ) ) { q . add ( new Pair < Integer , Integer > ( new Integer ( ( first - 1 ) ) , new Integer ( second ) ) ) ; inQueue [ ( first - 1 ) ] [ second ] = true ; } if ( ( ( ( ( ( first - 1 ) >= 0 ) && ( ( second + 1 ) <= ( height - 1 ) ) ) && ( ( visited [ ( first - 1 ) ] [ ( second + 1 ) ] ) == false ) ) && ( ( visitedThisTime [ ( first - 1 ) ] [ ( second + 1 ) ] ) == false ) ) && ( ( inQueue [ ( first - 1 ) ] [ ( second + 1 ) ] ) == false ) ) { q . add ( new Pair < Integer , Integer > ( new Integer ( ( first - 1 ) ) , new Integer ( ( second + 1 ) ) ) ) ; inQueue [ ( first - 1 ) ] [ ( second + 1 ) ] = true ; } if ( ( ( ( ( second - 1 ) >= 0 ) && ( ( visited [ first ] [ ( second - 1 ) ] ) == false ) ) && ( ( visitedThisTime [ first ] [ ( second - 1 ) ] ) == false ) ) && ( ( inQueue [ first ] [ ( second - 1 ) ] ) == false ) ) { q . add ( new Pair < Integer , Integer > ( new Integer ( first ) , new Integer ( ( second - 1 ) ) ) ) ; inQueue [ first ] [ ( second - 1 ) ] = true ; } if ( ( ( ( ( second + 1 ) <= ( height - 1 ) ) && ( ( visited [ first ] [ ( second + 1 ) ] ) == false ) ) && ( ( visitedThisTime [ first ] [ ( second + 1 ) ] ) == false ) ) && ( ( inQueue [ first ] [ ( second + 1 ) ] ) == false ) ) { q . add ( new Pair < Integer , Integer > ( new Integer ( first ) , new Integer ( ( second + 1 ) ) ) ) ; inQueue [ first ] [ ( second + 1 ) ] = true ; } if ( ( ( ( ( ( first + 1 ) <= ( width - 1 ) ) && ( ( second - 1 ) >= 0 ) ) && ( ( visited [ ( first + 1 ) ] [ ( second - 1 ) ] ) == false ) ) && ( ( visitedThisTime [ ( first + 1 ) ] [ ( second - 1 ) ] ) == false ) ) && ( ( inQueue [ ( first + 1 ) ] [ ( second - 1 ) ] ) == false ) ) { q . add ( new Pair < Integer , Integer > ( new Integer ( ( first + 1 ) ) , new Integer ( ( second - 1 ) ) ) ) ; inQueue [ ( first + 1 ) ] [ ( second - 1 ) ] = true ; } if ( ( ( ( ( first + 1 ) <= ( width - 1 ) ) && ( ( visited [ ( first + 1 ) ] [ second ] ) == false ) ) && ( ( visitedThisTime [ ( first + 1 ) ] [ second ] ) == false ) ) && ( ( inQueue [ ( first + 1 ) ] [ second ] ) == false ) ) { q . add ( new Pair < Integer , Integer > ( new Integer ( ( first + 1 ) ) , new Integer ( second ) ) ) ; inQueue [ ( first + 1 ) ] [ second ] = true ; } <START_BUG> if ( ( ( ( ( ( first + 1 ) <= ( width - 1 ) ) && ( ( second + 1 ) <= ( height - 1 ) ) ) && ( ( visited [ ( first + 1 ) ] [ ( second + 1 ) ] ) == false ) ) && ( ( visited [ ( first + 1 ) ] [ ( second + 1 ) ] ) == false ) ) && ( ( inQueue [ ( first + 1 ) ] [ ( second + 1 ) ] ) == false ) ) { <END_BUG> q . add ( new Pair < Integer , Integer > ( new Integer ( ( first + 1 ) ) , new Integer ( ( second + 1 ) ) ) ) ; inQueue [ ( first + 1 ) ] [ ( second + 1 ) ] = true ; } } } if ( count >= threshold ) { this . coherencePair [ b ] [ 0 ] += count ; } else { this . coherencePair [ b ] [ 1 ] += count ; } }

Fix:
if ( ( ( ( ( ( first + 1 ) <= ( width - 1 ) ) && ( ( second + 1 ) <= ( height - 1 ) ) ) && ( ( visited [ ( first + 1 ) ] [ ( second + 1 ) ] ) == false ) ) && ( ( visitedThisTime [ ( first + 1 ) ] [ ( second + 1 ) ] ) == false ) ) && ( ( inQueue [ ( first + 1 ) ] [ ( second + 1 ) ] ) == false ) ) {