import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int r = stdin.nextInt();
    int c = stdin.nextInt();
    int[][] sheet = new int[r + 1][c + 1];

    int sum = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    }

    sheet[r][c] = sum;
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    }
  }

} import java.util.Scanner; Scanner java.util public class Main {

  public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int r = stdin.nextInt();
    int c = stdin.nextInt();
    int[][] sheet = new int[r + 1][c + 1];

    int sum = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    }

    sheet[r][c] = sum;
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    }
  }

} Main public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int r = stdin.nextInt();
    int c = stdin.nextInt();
    int[][] sheet = new int[r + 1][c + 1];

    int sum = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    }

    sheet[r][c] = sum;
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    }
  } main {
    Scanner stdin = new Scanner(System.in);
    int r = stdin.nextInt();
    int c = stdin.nextInt();
    int[][] sheet = new int[r + 1][c + 1];

    int sum = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    }

    sheet[r][c] = sum;
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    }
  } Scanner stdin = new Scanner(System.in); stdin new Scanner(System.in) int r = stdin.nextInt(); r stdin.nextInt() stdin.nextInt stdin int c = stdin.nextInt(); c stdin.nextInt() stdin.nextInt stdin int[][] sheet = new int[r + 1][c + 1]; sheet new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 int sum = 0; sum 0 for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    } {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    } for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      } {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      } int n = stdin.nextInt(); n stdin.nextInt() stdin.nextInt stdin sheet[i][j] = n sheet[i][j] sheet[i] sheet i j n sheet[r][j] += n sheet[r][j] sheet[r] sheet r j n sheet[i][c] += n sheet[i][c] sheet[i] sheet i c n sum += n sum n sheet[r][c] = sum sheet[r][c] sheet[r] sheet r c sum for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    } int i = 0; int i = 0; i 0 i <= r i r i++ i++ i {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    } {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    } for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      } int j = 0; int j = 0; j 0 j <= c j c j++ j++ j {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      } {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      } System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]) System.out.printf System.out System System.out j < c ? "%d " : "%d%n" j < c j c "%d " "%d%n" sheet[i][j] sheet[i] sheet i j String[] args args public class Main {

  public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int r = stdin.nextInt();
    int c = stdin.nextInt();
    int[][] sheet = new int[r + 1][c + 1];

    int sum = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    }

    sheet[r][c] = sum;
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    }
  }

} public class Main {

  public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int r = stdin.nextInt();
    int c = stdin.nextInt();
    int[][] sheet = new int[r + 1][c + 1];

    int sum = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    }

    sheet[r][c] = sum;
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    }
  }

} Main
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int r = stdin.nextInt();
    int c = stdin.nextInt();
    int[][] sheet = new int[r + 1][c + 1];

    int sum = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int n = stdin.nextInt();
        sheet[i][j] = n;
        sheet[r][j] += n;
        sheet[i][c] += n;
        sum += n;
      }
    }

    sheet[r][c] = sum;
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        System.out.printf(j < c ? "%d " : "%d%n", sheet[i][j]);
      }
    }
  }

}