function solution(maps) {
var count = 0;
const dx=[0, 1, 0, -1];
const dy=[-1, 0, 1, 0];
let m=maps.length;
let n=maps[0].length;
let visited=Array.from({length: m}, ()=>new Array(n).fill(false));
visited[0][0]=1;
let queue=[[0,0]];
while(queue.length){
let [currentY, currentX]=queue.shift();
for (let i=0;i<4;i++){
let nextY=currentY+dy[i];
let nextX=currentX+dx[i];
if (nextY<0 || nextX<0 || nextY>=m || nextX>=n || maps[nextY][nextX]===0){
continue;
}
if (visited[nextY][nextX]){
continue;
}
queue.push([nextY, nextX])
visited[nextY][nextX]=visited[currentY][currentX]+1;
}
}
count = visited[maps.length - 1][maps[0].length - 1];
if (count===false){
return -1;
}else{
return count;
}
}