Traverse through matrix and find pairs p1 and p2
for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ i ] [ j ] == ' ' ) if ( p1 [ 0 ] == - 1 ) p1 = [ i , j ] ; else p2 = [ i , j ] ; } } p3 = p1 ; p4 = p2 ;