const m = [
[0, 1, 0, 0, 0, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 3, 0, 0, 0, 0, 0],
[0, 0, 4, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 4, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 4, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 4, 0, 0, 3, 0, 0, 0, 0],
[0, 0, 4, 0, 0, 0, 0, 3, 0, 0],
[0, 0, 4, 0, 3, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 4],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1]
]
const count = matrix.flat().reduce((acc, n) => (acc[n] = -~acc[n], acc), {});
const count = {};
for (const row of matrix) {
for (const n of row) {
if (!count.hasOwnProperty(n)) {
count[n] = 0;
}
count[n]++;
}
}