/* ITP1_7_C */
import java.io.*;

class Main
{
  public static void main(String[] args)
  {
    try{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringBuilder sb = new StringBuilder();

      String[] input = br.readLine().split(" ");
      int row = Integer.parseInt(input[0]);
      int column = Integer.parseInt(input[1]);
      int[][] matrix = new int[row + 1][column + 1];
      int all_sum = 0;

      // input & row sum
      for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      }
      // column sum
      for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      }
      matrix[row][column] = all_sum;

      // output
      for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      }
      System.out.print(sb);

    }catch(Exception e){
      System.out.println(e);
    }
  }
} import java.io.*; io.* java class Main
{
  public static void main(String[] args)
  {
    try{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringBuilder sb = new StringBuilder();

      String[] input = br.readLine().split(" ");
      int row = Integer.parseInt(input[0]);
      int column = Integer.parseInt(input[1]);
      int[][] matrix = new int[row + 1][column + 1];
      int all_sum = 0;

      // input & row sum
      for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      }
      // column sum
      for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      }
      matrix[row][column] = all_sum;

      // output
      for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      }
      System.out.print(sb);

    }catch(Exception e){
      System.out.println(e);
    }
  }
} Main public static void main(String[] args)
  {
    try{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringBuilder sb = new StringBuilder();

      String[] input = br.readLine().split(" ");
      int row = Integer.parseInt(input[0]);
      int column = Integer.parseInt(input[1]);
      int[][] matrix = new int[row + 1][column + 1];
      int all_sum = 0;

      // input & row sum
      for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      }
      // column sum
      for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      }
      matrix[row][column] = all_sum;

      // output
      for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      }
      System.out.print(sb);

    }catch(Exception e){
      System.out.println(e);
    }
  } main {
    try{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringBuilder sb = new StringBuilder();

      String[] input = br.readLine().split(" ");
      int row = Integer.parseInt(input[0]);
      int column = Integer.parseInt(input[1]);
      int[][] matrix = new int[row + 1][column + 1];
      int all_sum = 0;

      // input & row sum
      for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      }
      // column sum
      for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      }
      matrix[row][column] = all_sum;

      // output
      for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      }
      System.out.print(sb);

    }catch(Exception e){
      System.out.println(e);
    }
  } try{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringBuilder sb = new StringBuilder();

      String[] input = br.readLine().split(" ");
      int row = Integer.parseInt(input[0]);
      int column = Integer.parseInt(input[1]);
      int[][] matrix = new int[row + 1][column + 1];
      int all_sum = 0;

      // input & row sum
      for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      }
      // column sum
      for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      }
      matrix[row][column] = all_sum;

      // output
      for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      }
      System.out.print(sb);

    }catch(Exception e){
      System.out.println(e);
    } catch(Exception e){
      System.out.println(e);
    } Exception e {
      System.out.println(e);
    } System.out.println(e) System.out.println System.out System System.out e {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringBuilder sb = new StringBuilder();

      String[] input = br.readLine().split(" ");
      int row = Integer.parseInt(input[0]);
      int column = Integer.parseInt(input[1]);
      int[][] matrix = new int[row + 1][column + 1];
      int all_sum = 0;

      // input & row sum
      for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      }
      // column sum
      for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      }
      matrix[row][column] = all_sum;

      // output
      for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      }
      System.out.print(sb);

    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringBuilder sb = new StringBuilder(); sb new StringBuilder() String[] input = br.readLine().split(" "); input br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int row = Integer.parseInt(input[0]); row Integer.parseInt(input[0]) Integer.parseInt Integer input[0] input 0 int column = Integer.parseInt(input[1]); column Integer.parseInt(input[1]) Integer.parseInt Integer input[1] input 1 int[][] matrix = new int[row + 1][column + 1]; matrix new int[row + 1][column + 1] row + 1 row 1 column + 1 column 1 int all_sum = 0; all_sum 0 for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      } int i = 0; int i = 0; i 0 i < row i row i++ i++ i {
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      } {
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      } input = br.readLine().split(" ") input br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        } int j = 0; int j = 0; j 0 j < column j column j++ j++ j {
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        } {
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        } matrix[i][j] = Integer.parseInt(input[j]) matrix[i][j] matrix[i] matrix i j Integer.parseInt(input[j]) Integer.parseInt Integer input[j] input j matrix[i][column] += matrix[i][j] matrix[i][column] matrix[i] matrix i column matrix[i][j] matrix[i] matrix i j all_sum += matrix[i][j] all_sum matrix[i][j] matrix[i] matrix i j for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      } int j = 0; int j = 0; j 0 j < column j column j++ j++ j {
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      } {
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      } for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        } int i = 0; int i = 0; i 0 i < row i row i++ i++ i {
          matrix[row][j] += matrix[i][j];
        } {
          matrix[row][j] += matrix[i][j];
        } matrix[row][j] += matrix[i][j] matrix[row][j] matrix[row] matrix row j matrix[i][j] matrix[i] matrix i j matrix[row][column] = all_sum matrix[row][column] matrix[row] matrix row column all_sum for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      } int i = 0; int i = 0; i 0 i < (row+1) i (row+1) row 1 i++ i++ i {
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      } {
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      } for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        } int j = 0; int j = 0; j 0 j < (column+1) j (column+1) column 1 j++ j++ j {
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        } {
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        } if(j != 0) sb.append(" "); j != 0 j 0 sb.append(" ") sb.append sb " " sb.append(matrix[i][j]) sb.append sb matrix[i][j] matrix[i] matrix i j sb.append("\n") sb.append sb "\n" System.out.print(sb) System.out.print System.out System System.out sb String[] args args
/* ITP1_7_C */
import java.io.*;

class Main
{
  public static void main(String[] args)
  {
    try{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringBuilder sb = new StringBuilder();

      String[] input = br.readLine().split(" ");
      int row = Integer.parseInt(input[0]);
      int column = Integer.parseInt(input[1]);
      int[][] matrix = new int[row + 1][column + 1];
      int all_sum = 0;

      // input & row sum
      for(int i = 0; i < row; i++){
        input = br.readLine().split(" ");
        for(int j = 0; j < column; j++){
          matrix[i][j] = Integer.parseInt(input[j]);
          matrix[i][column] += matrix[i][j];
          all_sum += matrix[i][j];
        }
      }
      // column sum
      for(int j = 0; j < column; j++){
        for(int i = 0; i < row; i++){
          matrix[row][j] += matrix[i][j];
        }
      }
      matrix[row][column] = all_sum;

      // output
      for(int i = 0; i < (row+1); i++){
        for(int j = 0; j < (column+1); j++){
          if(j != 0) sb.append(" ");
          sb.append(matrix[i][j]);
        }
        sb.append("\n");
      }
      System.out.print(sb);

    }catch(Exception e){
      System.out.println(e);
    }
  }
}