var shortestPath = function(matrix, k) {
const numRows = matrix.length;
const numCols = matrix[0].length;
if (numRows === 1 && numCols === 1) {
const visited = new Set();
while (queue.length > 0) {
const currentCell = queue.shift();
const [currentRow, currentCol, kRemaining, numSteps] = currentCell;
const directions = [[0,1],[1,0],[0,-1],[-1,0]];
for (let direction of directions) {
const [rowOffset, colOffset] = direction;
const newRow = currentRow + rowOffset;
const newCol = currentCol + colOffset;
const isInBounds = newRow >= 0 && newRow < numRows && newCol >= 0 && newCol < numCols;
if (matrix[newRow][newCol] === 1 && kRemaining > 0 && !visited.has(`${newRow}-${newCol}-${kRemaining-1}`)) {
visited.add(`${newRow}-${newCol}-${kRemaining-1}`);
queue.push([newRow, newCol, kRemaining - 1, numSteps + 1]);
} else if (matrix[newRow][newCol] === 0 && !visited.has(`${newRow}-${newCol}-${kRemaining}`)) {
if (newRow === numRows - 1 && newCol === numCols - 1) {
visited.add(`${newRow}-${newCol}-${kRemaining}`);
queue.push([newRow, newCol, kRemaining, numSteps + 1]);