import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int line;
    int row;
    int[][] matrix = new int[r + 1][c + 1];
    for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    }
    for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    }
    for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    }
  }
}
 import java.util.*; util.* java public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int line;
    int row;
    int[][] matrix = new int[r + 1][c + 1];
    for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    }
    for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    }
    for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    }
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int line;
    int row;
    int[][] matrix = new int[r + 1][c + 1];
    for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    }
    for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    }
    for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    }
  } main {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int line;
    int row;
    int[][] matrix = new int[r + 1][c + 1];
    for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    }
    for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    }
    for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    }
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int line; line int row; row int[][] matrix = new int[r + 1][c + 1]; matrix new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    } {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    } line = 0 line 0 for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      } {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      } matrix[i][j] = sc.nextInt() matrix[i][j] matrix[i] matrix i j sc.nextInt() sc.nextInt sc line += matrix[i][j] line matrix[i][j] matrix[i] matrix i j matrix[i][c] = line matrix[i][c] matrix[i] matrix i c line for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    } int i = 0; int i = 0; i 0 i < c + 1 i c + 1 c 1 i++ i++ i {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    } {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    } row = 0 row 0 for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      } int j = 0; int j = 0; j 0 j < r j r j++ j++ j {
        row += matrix[j][i];
      } {
        row += matrix[j][i];
      } row += matrix[j][i] row matrix[j][i] matrix[j] matrix j i matrix[r][i] = row matrix[r][i] matrix[r] matrix r i row for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    } int i = 0; int i = 0; i 0 i < r + 1 i r + 1 r 1 i++ i++ i {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    } {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    } for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      } int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      } {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      } if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        } j == c j c {
          System.out.println(matrix[i][j]);
        } System.out.println(matrix[i][j]) System.out.println System.out System System.out matrix[i][j] matrix[i] matrix i j {
          System.out.print(matrix[i][j] + " ");
        } System.out.print(matrix[i][j] + " ") System.out.print System.out System System.out matrix[i][j] + " " matrix[i][j] matrix[i] matrix i j " " String[] args args public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int line;
    int row;
    int[][] matrix = new int[r + 1][c + 1];
    for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    }
    for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    }
    for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    }
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int line;
    int row;
    int[][] matrix = new int[r + 1][c + 1];
    for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    }
    for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    }
    for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    }
  }
} Main
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int line;
    int row;
    int[][] matrix = new int[r + 1][c + 1];
    for(int i = 0; i < r; i++) {
      line = 0;
      for(int j = 0; j < c; j++) {
        matrix[i][j] = sc.nextInt();
        line += matrix[i][j];
      }
      matrix[i][c] = line;
    }
    for(int i = 0; i < c + 1; i++) {
      row = 0;
      for(int j = 0; j < r; j++) {
        row += matrix[j][i];
      }
      matrix[r][i] = row;
    }
    for(int i = 0; i < r + 1; i++) {
      for(int j = 0; j < c + 1; j++) {
        if(j == c) {
          System.out.println(matrix[i][j]);
        } else {
          System.out.print(matrix[i][j] + " ");
        }
      }
    }
  }
}

