import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    StringBuilder output = new StringBuilder();
    String crlf = System.getProperty("line.separator");
    int row, col, input;
    int sumRow, sumMat;
    int[] sumCol;
    
    row = in.nextInt();
    col = in.nextInt();
    sumCol = new int[col];
    sumMat = 0;
    
    for (int i = 0; i < row; i++) {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    }
    for (int k = 0; k < col; k++) {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    }
    output.append(sumMat);
    System.out.println(output.toString());
  }
} import java.util.Scanner; Scanner java.util class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    StringBuilder output = new StringBuilder();
    String crlf = System.getProperty("line.separator");
    int row, col, input;
    int sumRow, sumMat;
    int[] sumCol;
    
    row = in.nextInt();
    col = in.nextInt();
    sumCol = new int[col];
    sumMat = 0;
    
    for (int i = 0; i < row; i++) {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    }
    for (int k = 0; k < col; k++) {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    }
    output.append(sumMat);
    System.out.println(output.toString());
  }
} Main public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    StringBuilder output = new StringBuilder();
    String crlf = System.getProperty("line.separator");
    int row, col, input;
    int sumRow, sumMat;
    int[] sumCol;
    
    row = in.nextInt();
    col = in.nextInt();
    sumCol = new int[col];
    sumMat = 0;
    
    for (int i = 0; i < row; i++) {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    }
    for (int k = 0; k < col; k++) {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    }
    output.append(sumMat);
    System.out.println(output.toString());
  } main {
    Scanner in = new Scanner(System.in);
    StringBuilder output = new StringBuilder();
    String crlf = System.getProperty("line.separator");
    int row, col, input;
    int sumRow, sumMat;
    int[] sumCol;
    
    row = in.nextInt();
    col = in.nextInt();
    sumCol = new int[col];
    sumMat = 0;
    
    for (int i = 0; i < row; i++) {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    }
    for (int k = 0; k < col; k++) {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    }
    output.append(sumMat);
    System.out.println(output.toString());
  } Scanner in = new Scanner(System.in); in new Scanner(System.in) StringBuilder output = new StringBuilder(); output new StringBuilder() String crlf = System.getProperty("line.separator"); crlf System.getProperty("line.separator") System.getProperty System "line.separator" int row row col col input; input int sumRow sumRow sumMat; sumMat int[] sumCol; sumCol row = in.nextInt() row in.nextInt() in.nextInt in col = in.nextInt() col in.nextInt() in.nextInt in sumCol = new int[col] sumCol new int[col] col sumMat = 0 sumMat 0 for (int i = 0; i < row; i++) {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    } int i = 0; int i = 0; i 0 i < row i row i++ i++ i {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    } {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    } sumRow = 0 sumRow 0 for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      } int j = 0; int j = 0; j 0 j < col j col j++ j++ j {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      } {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      } input =in.nextInt() input in.nextInt() in.nextInt in sumRow += input sumRow input sumCol[j] += input sumCol[j] sumCol j input output.append(input).append(" ") output.append(input).append output.append(input) output.append output input " " output.append(sumRow).append(crlf) output.append(sumRow).append output.append(sumRow) output.append output sumRow crlf for (int k = 0; k < col; k++) {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    } int k = 0; int k = 0; k 0 k < col k col k++ k++ k {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    } {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    } output.append(sumCol[k]).append(" ") output.append(sumCol[k]).append output.append(sumCol[k]) output.append output sumCol[k] sumCol k " " sumMat += sumCol[k] sumMat sumCol[k] sumCol k output.append(sumMat) output.append output sumMat System.out.println(output.toString()) System.out.println System.out System System.out output.toString() output.toString output String[] args args
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    StringBuilder output = new StringBuilder();
    String crlf = System.getProperty("line.separator");
    int row, col, input;
    int sumRow, sumMat;
    int[] sumCol;
    
    row = in.nextInt();
    col = in.nextInt();
    sumCol = new int[col];
    sumMat = 0;
    
    for (int i = 0; i < row; i++) {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    }
    for (int k = 0; k < col; k++) {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    }
    output.append(sumMat);
    System.out.println(output.toString());
  }
}