import java.util.Scanner;
import java.util.function.Function;

public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    Scanner sc = new Scanner(System.in);

    void solve() {
        int r, c;
        int[][] input, output;
        r = sc.nextInt();
        c = sc.nextInt();
        input = new int[r][];
        for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        }

        output = new int[r + 1][c + 1];
        int sum = 0;
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        }

        //h sum
        for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        }

        //v sum
        for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        }

        //sum
        output[r][c] = sum;

        for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        }

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

    public static void main(String[] args) {
        new Main().solve();
    }

    Scanner sc = new Scanner(System.in);

    void solve() {
        int r, c;
        int[][] input, output;
        r = sc.nextInt();
        c = sc.nextInt();
        input = new int[r][];
        for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        }

        output = new int[r + 1][c + 1];
        int sum = 0;
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        }

        //h sum
        for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        }

        //v sum
        for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        }

        //sum
        output[r][c] = sum;

        for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        }

    }
} Main Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
        new Main().solve();
    } main {
        new Main().solve();
    } new Main().solve() new Main().solve new Main() String[] args args void solve() {
        int r, c;
        int[][] input, output;
        r = sc.nextInt();
        c = sc.nextInt();
        input = new int[r][];
        for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        }

        output = new int[r + 1][c + 1];
        int sum = 0;
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        }

        //h sum
        for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        }

        //v sum
        for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        }

        //sum
        output[r][c] = sum;

        for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        }

    } solve {
        int r, c;
        int[][] input, output;
        r = sc.nextInt();
        c = sc.nextInt();
        input = new int[r][];
        for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        }

        output = new int[r + 1][c + 1];
        int sum = 0;
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        }

        //h sum
        for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        }

        //v sum
        for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        }

        //sum
        output[r][c] = sum;

        for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        }

    } int r r c; c int[][] input input output; output r = sc.nextInt() r sc.nextInt() sc.nextInt sc c = sc.nextInt() c sc.nextInt() sc.nextInt sc input = new int[r][] input new int[r][] r for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        } int i = 0; int i = 0; i 0 i < r i r ++i ++i i {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        } {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        } input[i] = new int[c] input[i] input i new int[c] c for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            } int j = 0; int j = 0; j 0 j < c j c ++j ++j j {
                input[i][j] = sc.nextInt();
            } {
                input[i][j] = sc.nextInt();
            } input[i][j] = sc.nextInt() input[i][j] input[i] input i j sc.nextInt() sc.nextInt sc output = new int[r + 1][c + 1] output 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) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        } int i = 0; int i = 0; i 0 i < r i r ++i ++i i {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        } {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        } for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            } int j = 0; int j = 0; j 0 j < c j c ++j ++j j {
                output[i][j] = input[i][j];
                sum += input[i][j];
            } {
                output[i][j] = input[i][j];
                sum += input[i][j];
            } output[i][j] = input[i][j] output[i][j] output[i] output i j input[i][j] input[i] input i j sum += input[i][j] sum input[i][j] input[i] input i j for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        } int i = 0; int i = 0; i 0 i < r i r ++i ++i i {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        } {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        } int hsum = 0; hsum 0 for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            } int j = 0; int j = 0; j 0 j < c j c ++j ++j j {
                hsum += output[i][j];
            } {
                hsum += output[i][j];
            } hsum += output[i][j] hsum output[i][j] output[i] output i j output[i][c] = hsum output[i][c] output[i] output i c hsum for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        } int j = 0; int j = 0; j 0 j < c j c ++j ++j j {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        } {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        } int vsum = 0; vsum 0 for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            } int i = 0; int i = 0; i 0 i < r i r ++i ++i i {
                vsum += output[i][j];
            } {
                vsum += output[i][j];
            } vsum += output[i][j] vsum output[i][j] output[i] output i j output[r][j] = vsum output[r][j] output[r] output r j vsum output[r][c] = sum output[r][c] output[r] output r c sum for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        } int i = 0; int i = 0; i 0 i <= r i r ++i ++i i {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        } {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        } for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            } int j = 0; int j = 0; j 0 j < c j c ++j ++j j {
                System.out.print(output[i][j] + " ");
            } {
                System.out.print(output[i][j] + " ");
            } System.out.print(output[i][j] + " ") System.out.print System.out System System.out output[i][j] + " " output[i][j] output[i] output i j " " System.out.println(output[i][c]) System.out.println System.out System System.out output[i][c] output[i] output i c public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    Scanner sc = new Scanner(System.in);

    void solve() {
        int r, c;
        int[][] input, output;
        r = sc.nextInt();
        c = sc.nextInt();
        input = new int[r][];
        for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        }

        output = new int[r + 1][c + 1];
        int sum = 0;
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        }

        //h sum
        for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        }

        //v sum
        for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        }

        //sum
        output[r][c] = sum;

        for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        }

    }
} public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    Scanner sc = new Scanner(System.in);

    void solve() {
        int r, c;
        int[][] input, output;
        r = sc.nextInt();
        c = sc.nextInt();
        input = new int[r][];
        for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        }

        output = new int[r + 1][c + 1];
        int sum = 0;
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        }

        //h sum
        for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        }

        //v sum
        for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        }

        //sum
        output[r][c] = sum;

        for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        }

    }
} Main
import java.util.Scanner;
import java.util.function.Function;

public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    Scanner sc = new Scanner(System.in);

    void solve() {
        int r, c;
        int[][] input, output;
        r = sc.nextInt();
        c = sc.nextInt();
        input = new int[r][];
        for (int i = 0; i < r; ++i) {
            input[i] = new int[c];
            for (int j = 0; j < c; ++j) {
                input[i][j] = sc.nextInt();
            }
        }

        output = new int[r + 1][c + 1];
        int sum = 0;
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                output[i][j] = input[i][j];
                sum += input[i][j];
            }
        }

        //h sum
        for (int i = 0; i < r; ++i) {
            int hsum = 0;
            for (int j = 0; j < c; ++j) {
                hsum += output[i][j];
            }
            output[i][c] = hsum;
        }

        //v sum
        for (int j = 0; j < c; ++j) {
            int vsum = 0;
            for (int i = 0; i < r; ++i) {
                vsum += output[i][j];
            }
            output[r][j] = vsum;
        }

        //sum
        output[r][c] = sum;

        for (int i = 0; i <= r; ++i) {
            for (int j = 0; j < c; ++j) {
                System.out.print(output[i][j] + " ");
            }
            System.out.println(output[i][c]);
        }

    }
}
