import java.io.IOException;
import java.util.Scanner;
 
class Main {
 
    public static void main(String[] args) throws IOException {
 
        Scanner scanner = new Scanner(System.in);
 
        int r = scanner.nextInt();
        int c = scanner.nextInt();
        int[][] list = new int[r][c];
        int[] sumR = new int[r];
        int[] sumC = new int[c];
        int sumRC = 0;
 
        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[0].length; j++) {
                list[i][j] = scanner.nextInt();
            }
        }
 
        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[0].length; j++) {
                sumR[i] += list[i][j];
            }
        }
         
        for (int i = 0; i < list[0].length; i++) {
            for (int j = 0; j < list.length; j++) {
                sumC[i] += list[j][i];
            }
            sumRC += sumC[i];
        }
 
        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[0].length; j++) {
                System.out.print(list[i][j] + " ");
            }
            System.out.println(sumR[i] + "");
        }
         
        for (int num : sumC) {
            System.out.print(num + " ");
        }
        System.out.print(sumRC + "\n");
    }
}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz