check source and destination cell of the matrix have value 1
if ( mat [ src . x ] [ src . y ] != 1 mat [ dest . x ] [ dest . y ] != 1 ) return - 1 ; boolean [ ] [ ] visited = new boolean [ ROW ] [ COL ] ;