Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}

		input := make([][]int, n+1, n+1)
		totalRow := make([]int, n+1, n+1)

		for i := 0; i < n; i++ {
			row := make([]int, n+1, n+1)
			scanner.Scan()

			row = numbers(scanner.Text())

			input[i] = row
			total(totalRow, row)
		}

		input[n] = totalRow

		for xk := range input {
			for yk, yv := range input[xk] {
				if yk == n {
					fmt.Printf("%5d\n", yv)
				} else {
					fmt.Printf("%5d", yv)
				}
			}
		}
	}
}

func total(sum, row []int) {
	for i := 0; i < len(row); i++ {
		sum[i] += row[i]
	}
}

func numbers(s string) []int {
	var n []int
	var sum int
	for _, f := range strings.Fields(s) {
		i, err := strconv.Atoi(f)
		if err == nil {
			n = append(n, i)
			sum += i
		}
	}
	n = append(n, sum)
	return n
}

Python: while 1:
    try:
        N=int(input())
        if N==0:break
        matrix = [list(map(int,input().split())) for i in range(N)]
        matrix.append([])
        for i in range(N):matrix[i].append(sum(matrix[i]))
        for j in range(len(matrix)):
            pointer=0
            for k in range(N):
                pointer +=matrix[k][j]
            matrix[N].append(pointer)
        for i in range(N+1):
            for j in range(N+1):
                print(str(matrix[i][j]).rjust(5), end="")
            print()
    except:break

Python: while True:
    n = int(input())
    if n == 0:
        break
    total = [0] * (n + 1)
    for i in range(n):
        a = [int(i) for i in input().split()]
        a.append(sum(a))
        for j in range(n + 1):
            print("%5d" % (a[j]), end = "")
            total[j] += a[j]
        print()
    for i in range(n + 1):
        print("%5d" % (total[i]), end = "")
    print()

Python: #!/usr/bin/python
# -*- coding: utf-8 -*-

def main():
    while(True):
        # 入力
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        for y in x:
            print("{0:4d}".format(y)),
        else:
            print

if __name__=='__main__':
    main()

Python: while True:
  l = []
  n = int(input())
  if n == 0:
    break
  lx = [0 for i in range(n+1)]
  for j in range(n):
    l1 = [int(i) for i in input().split()]
    c = 0
    for i in l1:
      c += i
    l1.append(c)
    for i in range(n+1):
      lx[i] += l1[i]
    l.append(l1)
  l.append(lx)
  for s in l:
    a = ''
    for t in s:
      a += ' ' * (5-len(str(t)))
      a += str(t)
    print(a)

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write

fmt = "{:5d}".format
def solve():
    N = int(readline())
    if N == 0:
        return False
    A = [list(map(int, readline().split())) for i in range(N)]
    for ln in A:
        write("".join(map(fmt, ln)))
        write(fmt(sum(ln)))
        write("\n")
    b = [sum(A[i][j] for i in range(N)) for j in range(N)]
    write("".join(map(fmt, b)))
    write(fmt(sum(b)))
    write("\n")
    return True
while solve():
    ...

C++: #include <stdio.h>

int main(void){
	int matrix[10][10],i,j,dataset;
	while(scanf("%d",&dataset)){

		if(dataset==0)break;

		//init add total norm
		for(i=0;i<dataset+1;i++){
			matrix[dataset][i]=0;
			matrix[i][dataset]=0;
		}

		//input and calc
		for(i=0;i<dataset;i++){
			for(j=0;j<dataset;j++){
				scanf("%d",&matrix[i][j]);
				matrix[i][dataset]+=matrix[i][j];
			}
		}

		//calc
		for(j=0;j<dataset+1;j++){
			for(i=0;i<dataset;i++){
				matrix[dataset][j]+=matrix[i][j];
			}
		}

		//output
                for(i=0;i<dataset+1;i++){
                        for(j=0;j<dataset+1;j++){
                                printf("%5d",matrix[i][j]);
                        }
			printf("\n");
                }
	}
}

C++: #include <stdio.h>

int main(){

  int n;

  while(1){
    scanf("%d", &n);
    if(n==0) break;

    int t[n+1][n+1];
    int i,j;
    for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d", &t[i][j]);

    t[n][n] = 0;

    for(i=0;i<n;i++){
      t[n][i] = 0;
      for(j=0;j<n;j++) t[n][i] += t[j][i];
      t[n][n] += t[n][i];
    }

    for(i=0;i<n;i++){
      t[i][n] = 0;
      for(j=0;j<n;j++) t[i][n] += t[i][j];
      t[n][n] += t[i][n];
    }

    t[n][n] /= 2;

    for(i=0;i<=n;i++){
      for(j=0;j<=n;j++) printf("%5d", t[i][j]);
      puts("");
    }
  }

  return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <sstream>
#include <cmath>
#include <climits>
#include <set>
#include <iostream>
#include <map>
#include <functional>
#include <cstdlib>
#include <numeric>
#include <queue>
#include <complex>
#include <sstream>
#include <stack>

using namespace std;

#define reep(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reep(i,0,n)

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef complex<double> Point;

typedef map<int, pair<int, long long> > mipill;

int main()
{
	int n;
	while(scanf("%d", &n), n){
		vi colums(n, 0);
		rep(i, n){
			int row = 0;
			rep(j, n){
				int a;
				scanf("%d", &a);
				printf("%5d", a);
				row += a;
				colums[j] += a;
			}
			printf("%5d\n", row);
		}

		int sum = 0;
		rep(i, n){
			printf("%5d", colums[i]);
			sum += colums[i];
		}
		printf("%5d\n", sum);
	}

	return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {

  int size;
  int matrix[10][10];

  int sum_row,sum_column,sum;
  char line[200];
  char* p;

  int i,j;
  char c;

  
  while(1) {

    scanf("%d",&size);
    c=getchar();

    if(size==0) break;

    for(i=0;i<size;i++) {

      fgets(line,sizeof(line),stdin);
      line[strlen(line)-1]='\0';

      p=strtok(line," ");
      matrix[i][0]=atoi(p);

      for(j=1;j<size;j++) {
	p=strtok(NULL," ");
	matrix[i][j]=atoi(p);
      }

    }

    for(i=0;i<size;i++) {
     
      sum_row=0;

      for(j=0;j<size;j++) {

	printf("%5d",matrix[i][j]);

	sum_row+=matrix[i][j];

      }

      printf("%5d\n",sum_row);
    }

    sum=0;

    for(j=0;j<size;j++) {

      sum_column=0;

      for(i=0;i<size;i++) {

	sum_column+=matrix[i][j];

      }

      sum+=sum_column;

      printf("%5d",sum_column);
    }

    printf("%5d\n",sum);

  }

  return 0;

}

C++: #include <stdio.h>
int main(){
	int n;
	while(1){
	    int l,r;
		int a[11][11];
		scanf("%d",&n);
		if(!n) break;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++) scanf("%d",&a[i][j]);
		}
		for(int i=0;i<n+1;i++){
			l=r=0;
			for(int j=0;j<n;j++){
				l+=a[i][j];
				r+=a[j][i];
			}
			a[i][n]=l;
			a[n][i]=r;
		}
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++) printf("%3d ",a[i][j]);
			printf("\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int n;
	
	while( cin >> n &&  n ){
		int* table = new int[ (n+1) * (n+1) ]();
		for( int i = 0; i < n; ++i ){
			int sum_col = 0;
			for( int j = 0; j < n; ++j ){
				int d;
				cin >> d;
				table[ i * (n+1) + j ] = d;
				table[ i * (n+1) + n ] += d;
				table[ n * (n+1) + j ] += d;
				table[ n * (n+1) + n ] += d;
			}
		}

		for( int i = 0; i < n+1; ++i ){
			for( int j = 0; j < n+1; ++j ){
				cout.width( 5 );
				cout << table[ i * (n+1) + j ];
			}
			cout << endl;
		}

		delete[] table;
	}
	return 0;
}