#K54182. Row and Column Sums
Row and Column Sums
Row and Column Sums
You are given a matrix \(A\) of dimensions \(n \times m\). Your task is to compute the sum of each row and each column. Formally, for each row \(i\) (with \(1 \leq i \leq n\)), calculate
[ R_i = \sum_{j=1}^{m} A_{ij} ]
and for each column \(j\) (with \(1 \leq j \leq m\)), calculate
[ C_j = \sum_{i=1}^{n} A_{ij} ]
Then, output the row sums in one line and the column sums in the next line. The input is read from standard input and the output should be written to standard output.
inputFormat
The first line of input contains two integers \(n\) and \(m\), representing the number of rows and columns respectively.
This is followed by \(n\) lines, each containing \(m\) integers, representing the matrix \(A\) in row-major order.
outputFormat
Output two lines:
- The first line contains \(n\) integers separated by spaces, representing the sums of each row.
- The second line contains \(m\) integers separated by spaces, representing the sums of each column.
3 3
1 2 3
4 5 6
7 8 9
6 15 24
12 15 18
</p>