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
}

C++: #include <iostream>
#include <stdio.h>

using namespace std;

int main(){
	int n,all=0;
	int data[512][512];
	int tdata[512] = { 0 };
	
	while (1){
		//input
		cin >> n;
		if (n == 0)break;

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> data[i][j];
			}
		}

		//output

		for (int i = 0; i < n; i++, all = 0){
			for (int j = 0; j < n; j++){
				printf(" %d", data[i][j]);
				all += data[i][j];
				tdata[j] += data[i][j];
			}
			printf(" %d\n", all);
		}

		all = 0;
		for (int i = 0; i < n; i++){
			printf(" %d", tdata[i]);
			all += tdata[i];
		}
		printf(" %d\n", all);
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;

int main(){
int N,num[11][11]={},x;
int a;
while(1)
{cin>>N;
if(N==0)break;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
{cin>>num[i][j];num[i][N]+=num[i][j];}
}
for(int i=0;i<N;i++)
num[N][N]+=num[i][N];
for(int i=0;i<N;i++)
	for(int j=0;j<N;j++)
num[N][i]+=num[j][i];

for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++)
{
a =5-( (int)log10((double)(num[i][j]) )+ 1);
for(int k=0;k<a;k++)
{cout<<" ";}
cout<<num[i][j];}
cout<<endl;


}

for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++){
num[i][j]=0;
}
}


}

return 0;
}

C++: #include <iostream>
#include <stdio.h>

using namespace std;

int main(){
	int n,all=0;
	int data[256][256];
	int tdata[256] = { 0 };
	
	while (1){
		//input
		cin >> n;
		if (n == 0)break;

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> data[i][j];
			}
		}

		//output

		for (int i = 0; i < n; i++, all = 0){
			for (int j = 0; j < n; j++){
				printf(" %3d", data[i][j]);
				all += data[i][j];
				tdata[j] += data[i][j];
			}
			printf(" %3d\n", all);
		}

		all = 0;
		for (int i = 0; i < n; i++){
			printf(" %3d", tdata[i]);
			all += tdata[i];
		}
		printf(" %3d\n", all);
	}
	return 0;
}

C++: #include <cstdio>

int main(void)
{
    int data[11][11];
    int n;
    
    while (1){
        scanf("%d", &n);
        if (n == 0) break;
        
        for (int i = 0; i <= n; i++){
            for (int j = 0; j <= n; j++){
                data[i][j] = 0;
            }
        }
        
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                scanf("%d", &data[i][j]);
                data[i][n] += data[i][j];
                data[n][j] += data[i][j];
                data[n][n] += data[i][j];
            }
        }
        
        for (int i = 0; i <= n; i++){
            for (int j = 0; j <= n; j++){
                printf("%5d", data[i][j]);
            }
            printf("\n");
        }
        
    }
    
    return (0);
}

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

int main(){
    int n;
    while(cin>>n , n){
        int table[n+1][n+1];
        
        for(int i=0; i<=n; i++){
            table[i][n]=0;
            table[n][i]=0;
        }
        
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                cin>>table[i][j];
            }
        }
        

        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
               table[i][n] += table[i][j];
            }
        }
        
        for(int j=0; j<=n; j++){
            for(int i=0; i<n; i++){
                table[n][j] += table[i][j];
            }
        }
        
        for(int i=0; i<=n; i++){
            for(int j=0; j<=n; j++){
                printf("%5d",table[i][j]);
            }
            cout<<endl;
        }
    }
    
    return 0;
}

C++: import java.util.*;
import java.lang.*;
import java.io.*;

public class Main{
	public static void main (String[] args) throws java.lang.Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    String line = br.readLine();
	    while(!line.equals("0")){
	        int n = Integer.parseInt(line);
	        int[] sum_arr = new int[n];
            int[][] table = new int[n+1][n+1];
	        for(int i=0;i<n;i++){
	            line = br.readLine();
	            String[] lines = line.split(" ");
	            int j=0;
	            int sum = 0;
	            for(String st:lines){
	                int tmp = Integer.parseInt(st);
	                table[i][j] = tmp;
	                sum += tmp;
	                sum_arr[j] += tmp;
	                ++j;
	            }
	            table[i][j] = sum;
	        }
	        int sum = 0;
	        for(int i=0;i<n;i++){
	            table[n][i] = sum_arr[i];
	            sum += sum_arr[i];
	        }
	        table[n][n] = sum;
	        
	        int length = Integer.toString(sum).length();
	        String fmt = "%" + length + "s";
	        for(int[] t:table){
	            int cnt = 0;
	            for(int val:t){
	                if(cnt>0){
    	                System.out.print(" ");
	                    cnt++;
	                }
	                System.out.print(String.format(fmt,Integer.toString(val)));
	            }
                System.out.println("");
	        }
            line = br.readLine();
	    }
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n;
	int data[101][101] = {0};
	cin >> n;
	rep(i,n)rep(j,n)
		cin >> data[i][j] , data[n][n] += data[i][j];
	rep(i,n){
		int s = 0;
		rep(j,n) s+= data[i][j];
		data[i][n] = s;
	}
	rep(i,n){
		int s = 0;
		rep(j,n) s+= data[j][i];
		data[n][i] = s;
	}
	rep(i,n+1){
		rep(j,n+1)
			printf("%5d",data[i][j]);
		cout << endl;
	}
}

C++: #include<iostream>
using namespace std;
int main(){
  int n;
  while(cin>>n){
    if(n==0) break;
    int s[n+1][n+1];
    for(int i=0;i<n;i++){
      int a = 0;
      for(int j=0;j<n;j++){
	cin >> s[i][j];
	a += s[i][j];
      }
      s[i][n] = a;
    }
    for(int i=0;i<n;i++){
      int a = 0;
      for(int j=0;j<n;j++){
	a += s[j][i];
      }
      s[n][i] = a;
    }
    int a = 0;
    for(int i=0;i<n;i++) a += s[n][i]; 
    s[n][n] = a;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	if(s[i][j]>=10000) cout << s[i][j] << endl;
	else if(s[i][j]>=1000) cout << " " << s[i][j];
	else if(s[i][j]>=100) cout << "  " << s[i][j];
	else if(s[i][j]>=10) cout << "   " << s[i][j];
	else if(s[i][j]>=0) cout << "    " << s[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}