#include <iostream>
using namespace std;
class xl {
	int row;
	int col;
	int **cells;
public:
	xl(int row,int col);
	~xl();
	void calcSum(void);
	int getCells(int r, int c);
	int setCells(int r, int c, int x);
};

int xl::getCells(int r, int c){
	return cells[r][c];
}
int xl::setCells(int r, int c, int x){
	cells[r][c] = x;
}
xl::xl(int row, int col){
	this->row = row;
	this->col = col;
	cells = new int*[row + 1];
	for(int i = 0; i < row + 1; i++){
		cells[i] = new int[col + 1];
	}
}
xl::~xl(){
	for(int i = 0; i < row + 1; i++){
		delete[] cells[i];	
	}
	delete[] cells;
}
void xl::calcSum(void){
	for(int i = 0; i < row; i++){
		int sum = 0;	
		for(int j = 0; j < col; j++){
			sum += cells[i][j];
		}
		cells[i][col] = sum;
	}
	for(int i = 0; i < col + 1; i++){
		int sum = 0;
		for(int j = 0; j < row; j++){
			sum += cells[j][i];
		}
		cells[row][i] = sum;
	}
}


int main(int argc,char *argv[]){
	int r,c;
	cin >> r >> c;
	xl *exl = new xl(r,c);
	for(int i = 0; i < r; i++){
		for(int j = 0;j < c; j++){
			int x;
			cin >> x;
			exl->setCells(i,j,x);
		}
	}
	exl->calcSum();
	for(int i = 0; i < r + 1; i++){
		for(int j = 0; j < c; j++){
			cout << exl->getCells(i,j) << " ";
		}
		cout << exl->getCells(i, c) << endl;
	}
	delete exl;
}
