import java.util.Scanner;
import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int r = s.nextInt();
    int c = s.nextInt();
    int[][] table = new int[r][c];
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    }
    s.close();
    for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    }
    int total = 0;
    for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    }
    System.out.print(total + "\n");
  }
}
 import java.util.Scanner; Scanner java.util import java.util.ArrayList; ArrayList java.util public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int r = s.nextInt();
    int c = s.nextInt();
    int[][] table = new int[r][c];
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    }
    s.close();
    for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    }
    int total = 0;
    for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    }
    System.out.print(total + "\n");
  }
} Main public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int r = s.nextInt();
    int c = s.nextInt();
    int[][] table = new int[r][c];
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    }
    s.close();
    for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    }
    int total = 0;
    for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    }
    System.out.print(total + "\n");
  } main {
    Scanner s = new Scanner(System.in);
    int r = s.nextInt();
    int c = s.nextInt();
    int[][] table = new int[r][c];
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    }
    s.close();
    for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    }
    int total = 0;
    for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    }
    System.out.print(total + "\n");
  } Scanner s = new Scanner(System.in); s new Scanner(System.in) int r = s.nextInt(); r s.nextInt() s.nextInt s int c = s.nextInt(); c s.nextInt() s.nextInt s int[][] table = new int[r][c]; table new int[r][c] r c for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    } {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    } for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
        table[i][j] = s.nextInt();
      } {
        table[i][j] = s.nextInt();
      } table[i][j] = s.nextInt() table[i][j] table[i] table i j s.nextInt() s.nextInt s s.close() s.close s for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    } {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    } int sum = 0; sum 0 for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      } int j = 0; int j = 0; j 0 j <= c j c j++ j++ j {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      } {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      } if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        } j < c j c {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } System.out.print(table[i][j] + " ") System.out.print System.out System System.out table[i][j] + " " table[i][j] table[i] table i j " " sum += table[i][j] sum table[i][j] table[i] table i j {
          System.out.println(sum);
        } System.out.println(sum) System.out.println System.out System System.out sum int total = 0; total 0 for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    } {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    } int colSum = 0; colSum 0 for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      } int i = 0; int i = 0; i 0 i <= r i r i++ i++ i {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      } {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      } if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        } i < r i r {
          colSum += table[i][j];
        } colSum += table[i][j] colSum table[i][j] table[i] table i j {
          System.out.print(colSum + " ");
        } System.out.print(colSum + " ") System.out.print System.out System System.out colSum + " " colSum " " total += colSum total colSum System.out.print(total + "\n") System.out.print System.out System System.out total + "\n" total "\n" String[] args args public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int r = s.nextInt();
    int c = s.nextInt();
    int[][] table = new int[r][c];
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    }
    s.close();
    for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    }
    int total = 0;
    for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    }
    System.out.print(total + "\n");
  }
} public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int r = s.nextInt();
    int c = s.nextInt();
    int[][] table = new int[r][c];
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    }
    s.close();
    for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    }
    int total = 0;
    for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    }
    System.out.print(total + "\n");
  }
} Main
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int r = s.nextInt();
    int c = s.nextInt();
    int[][] table = new int[r][c];
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        table[i][j] = s.nextInt();
      }
    }
    s.close();
    for (int i = 0; i < r; i++) {
      int sum = 0;
      for (int j = 0; j <= c; j++) {
        if (j < c) {
          System.out.print(table[i][j] + " ");
          sum += table[i][j];
        } else {
          System.out.println(sum);
        }
      }
    }
    int total = 0;
    for (int j = 0; j < c; j++) {
      int colSum = 0;
      for (int i = 0; i <= r; i++) {
        if (i < r) {
          colSum += table[i][j];
        } else {
          System.out.print(colSum + " ");
        }
      }
      total += colSum;
    }
    System.out.print(total + "\n");
  }
}

