Go: package main

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

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

		a := make([][]int, n+1)
		for i := 0; i < n; i++ {
			sum := 0
			stdin.Scan()
			a[i] = make([]int, n+1)
			str := strings.Split(stdin.Text(), " ")
			for j := 0; j < n; j++ {
				a[i][j], _ = strconv.Atoi(str[j])
				sum += a[i][j]
				fmt.Printf("%5d", a[i][j])
			}
			a[i][n] = sum
			fmt.Printf("%5d\n", a[i][n])
		}
		a[n] = make([]int, n+1)
		for i := 0; i <= n; i++ {
			sum := 0
			for j := 0; j < n; j++ {
				sum += a[j][i]
			}
			a[n][i] = sum
			fmt.Printf("%5d", a[n][i])
		}
		fmt.Println()
	}
}

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;
}

C++: #include <iostream>
#include<iomanip>

using namespace std;

int main() {
    
    int n;
    while (cin >> n) {
    
    if(n == 0)
    break;
    
    int A[51][51] = {};
    
    for(int i=0;i<n;i++) { 
    for(int j=0;j<n;j++) {
    	
    	cin >> A[i][j];
    	
    	A[i][n] += A[i][j];
    	A[n][j] += A[i][j];
	}
	A[n][n] += A[i][n];
}

	for(int i=0;i<=n;i++) { 
    for(int j=0;j<=n;j++) {
    	
    	cout << setw(4)<< A[i][j] << " ";
    
	}
	cout << "\n";
}
}
    return 0;
}

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

int main(){
	int n;
	while(cin >> n){
		if(n == 0) break;
		else{
			int data[n+2][n+2] = {0};

			for (int i = 0; i < n+2; ++i){
				for(int j = 0; j < n+2; j++){					
				data[i][j] = 0;
				}
			}
			
			for (int i = 0; i < n; ++i){
				for(int j = 0; j < n; j++){					
				cin >> data[i][j];
				}
			}
			for (int i = 0; i < n; ++i){
				for(int j = 0; j < n; j++){	
					data[i][n] += data[i][j];
				}
			}
			for (int i = 0; i < n+1; ++i){
				for(int j = 0; j < n; j++){	
					data[n][i] += data[j][i];
				}
			}
			for (int i = 0; i < n+1; ++i){
				for(int j = 0; j < n+1; j++){
					cout << data[i][j]<<" ";
				}
				cout << endl;
			}
				
		}
	}
	return 0;
}

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

int main(void){
    int n, a[11][11], i, j ;
    cin >> n;
    
    while(n != 0){
        //初期化
        for(i=0; i<=10; i++){
            for(j=0; j<=10; j++){
                a[i][j]=0;
            }
        }
        
        for(i=0; i<n; i++){
            for(j=0; j<n; j++){
                cin >> a[i][j];
                a[i][n] += a[i][j];
                a[n][j] += a[i][j];
                cout << setw(5) << a[i][j];
            }
            a[n][n] += a[i][n];
            cout << setw(5) << a[i][n] << endl;
        }
        for(j=0; j<=n; j++){
            cout << setw(5) << a[n][j];
        }
        cout << endl;
        cin >> n;
    }
    return 0;
}