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()
	}
}

C++: #include <iostream>
#include <cstdio>
const int MAX_N = 10;
using namespace std;

int main(void) {
  int n;
  while (1) {
    cin >> n;
    if (n == 0) break;
    int masu[MAX_N + 1][MAX_N + 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> masu[i][j];
      }
    }

    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        tmp += masu[i][j];
      }
      masu[i][n] = tmp;
    }
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        tmp += masu[j][i];
      }
      masu[n][i] = tmp;
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += masu[n][i];
    }
    masu[n][n] = sum;

    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n; j++) {
        printf("%5d ", masu[i][j]);
      }
      printf("%5d\n", masu[i][n]);
    }
  }

  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define rp(a) while(a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main(){
	int n;
	while(cin>>n,n){
		vector<vector<int> >in(n+1,vector<int>(n+1));
		rep(i,n)rep(j,n)cin>>in[i][j];
		rep(i,n){
			int sum=0;
			rep(j,n)sum+=in[i][j];
			in[i][n]=sum;
		}
		rep(i,n+1){
			int sum=0;
			rep(j,n)sum+=in[j][i];
			in[n][i]=sum;
		}
		rep(i,n+1){
			rep(j,n+1)printf("%5d",in[i][j]);
			cout<<endl;
		}
	}
}

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++: #!/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:
        print "".join(map(lambda x_: str(x_).rjust(5), x))

if __name__=='__main__':
    main()

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	int a[16][16];

	while (cin >> n, n){
		
		memset(a, 0, sizeof(a));

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

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

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

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

C++: #include<bits/stdc++.h>
using namespace std;

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

		int all_sum = 0;

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




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


		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				printf("%3d", data[i][j]);
				if(j != n) cout << " ";
			}
			cout << endl;
		}
	}
}

C++: #include<iostream>
#include<vector>
int main()
{
	while (1) {
		int sum=0;
		int num;
		std::cin >> num;
		if (num == 0)break;
		std::vector<std::vector<int>>vec(num + 1, std::vector<int>(num + 1));
		for (int i = 0; i < num; ++i) {
			for (int j = 0; j < num; ++j) {
				std::cin >> vec[i][j];
			}
		}
		for (int i = 0; i < num; ++i) {
			for (int j = 0; j < num; ++j) {
				vec[num][i] += vec[j][i];
				sum+= vec[j][i];
				vec[i][num] += vec[i][j];
			}
		}
		vec[num][num] = sum;
		for (int i = 0; i < num + 1; ++i) {
			for (int j = 0; j < num + 1; ++j) {
				if (j == num)std::cout << vec[i][j] << std::endl;
				else std::cout << vec[i][j] << " ";
			}
		}
	}
	return 0;
}

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

int main()
{
	int n;

	while (scanf("%d", &n), n){
		for (int i = 0; i < n; i++){
			int num, sum = 0;
			for (int j = 0; j < 4; j++){
				scanf("%d", &num);
				sum += num;
				printf("%5d", num);
			}
			printf("%5d\n", sum);
		}
	}

	return (0);
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>
 
using namespace std;
 
 
 
int main(){
 
    int n,tmp,row_sum,col_sum[11];
 
    while(true){
        scanf("%d",&n);
        if(n == 0)break;
 
        for(int i=0; i < 11; i++)col_sum[i] = 0;
 
        for(int i=0; i < n; i++){
            row_sum = 0;
            for(int k=0; k < n; k++){
                scanf("%d",&tmp);
                printf("%5d",tmp);
                row_sum += tmp;
                col_sum[k] += tmp;
            }
            printf("%5d\n",row_sum);
            col_sum[n] += row_sum;
        }
        for(int i=0; i <= n; i++)printf("%5d",col_sum[i]);
        printf("\n");
    }
 
    return 0;
}