/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (m) {
let w = m[0].length;
let h = m.length - 1;
const answer = [];
let row = 0;
let col = -1;
while (w >= 0 && h >= 0) {
col++;
for (let i = 0; i < w; i++) {
const curr = m[row][col];
answer.push(curr);
col++;
}
w--;
if (w < 0) break;
col--;
row++;
for (let i = 0; i < h; i++) {
const curr = m[row][col];
answer.push(curr);
row++;
}
h--;
if (h < 0) break;
row--;
col--;
for (let i = 0; i < w; i++) {
const curr = m[row][col];
answer.push(curr);
col--;
}
w--;
if (w < 0) break;
col++;
row--;
for (let i = 0; i < h; i++) {
const curr = m[row][col];
answer.push(curr);
row--;
}
h--;
if (h < 0) break;
row++;
}
return answer;
};