module.exports = function solveSudoku(matrix) {

  for ( let row = 0; row < 9; row++ ) {
    
    for ( let column = 0; column < 9; column++ ) {
      
      if ( matrix[row][column] === 0 ) {      
         
        const subSudokuRowInd = Math.floor(row / 3) * 3;  
        const subSudokuColInd = Math.floor(column / 3) * 3; 

        const allNumbers = [

          matrix[row][0], matrix[row][1], matrix[row][2], matrix[row][3],
          matrix[row][4], matrix[row][5], matrix[row][6], matrix[row][7], matrix[row][8],

          matrix[0][column], matrix[1][column], matrix[2][column], matrix[3][column],
          matrix[4][column], matrix[5][column], matrix[6][column], matrix[7][column], matrix[8][column],

          matrix[subSudokuRowInd + 0][subSudokuColInd + 0], matrix[subSudokuRowInd + 0][subSudokuColInd + 1], matrix[subSudokuRowInd + 0][subSudokuColInd + 2],
          matrix[subSudokuRowInd + 1][subSudokuColInd + 0], matrix[subSudokuRowInd + 1][subSudokuColInd + 1], matrix[subSudokuRowInd + 1][subSudokuColInd + 2],
          matrix[subSudokuRowInd + 2][subSudokuColInd + 0], matrix[subSudokuRowInd + 2][subSudokuColInd + 1], matrix[subSudokuRowInd + 2][subSudokuColInd + 2]
        ].filter((element) => { return element > 0 });

        const numbersToReplace0 = [1, 2, 3, 4, 5, 6, 7, 8, 9].filter( (element) => { 

          return allNumbers.indexOf(element) < 0;

        });   
        
        if (numbersToReplace0.length > 0) {

          for (let i = 0; i < numbersToReplace0.length; i++) {

            matrix[row][column] = numbersToReplace0[i];

            const solution = solveSudoku(matrix);

            if ( solution ) {   

              return solution; 

            };
          };

          matrix[row][column] = 0;
          
          return false; 
        } else { 
          
          return false; 
        };
      };
    };
  };

  return matrix;
}
