function maxdiags(m) {
var len = m.length;
var res = new Array(len * 2 - 1);
for (var i = 0; i < len; ++i) {
for (var j = 0; j < len; ++j) {
res[i + j] = Math.max(res[i + j] || -Infinity, m[i][j]);
}
}
return res;
}
var matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 8, 7, 6],
[5, 4, 3, 2]
];
console.log(matrix);
console.log(maxdiags(matrix));