galiog,
let array = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
],
[a, b, i, j, l] = [0, 1, 0, 0, 9],
arr = [];
for (let k = 0; k < l; k++) {
let v = array[i] && array[i][j];
if (v && !arr.includes(v)) {
arr.unshift(v);
} else {
k--;
i -= a;
j -= b;
[a, b] = a ? [b, -a] : [b, a]
};
i += a;
j += b;
}
console.log(arr)
alert(arr)