Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/matritsa-diagonallar-boylab

Diagonal traversal

Medium 1000 ms 64 MB 54%
Share

A matrix of size $n \times m$ is given. Print its elements diagonal by diagonal, starting from the top-left corner: first the cell with $i + j = 2$, then the cells with $i + j = 3$, then $i + j = 4$, and so on. Within one diagonal the cells go in increasing order of the row number ($i$).

A matrix: the down-left diagonals starting at the top-left corner are walked one after another along the arrows

Input

The first line contains two positive integers $n$ and $m$.
Each of the next $n$ lines contains $m$ integers — the matrix.

Output

One line with $n \cdot m$ space-separated integers.

Note

$1 \le n, m \le 100$
$-1000 \le a_{i,j} \le 1000$

Examples

Input #1
3 4
1 2 3 4
5 6 7 8
9 10 11 12
Output #1
1 2 5 3 6 9 4 7 10 8 11 12
Input #2
1 1
5
Output #2
5

Sign in to submit a solution

Signing up is free and takes a minute.