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<cstdio>
#include<iostream>

using namespace std;
  
main(){
  int n;
  cin>>n;
 while(n!=0){
    int d[n+1][n+1];

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

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

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

C++: #include <bits/stdc++.h>
#define MAX_N 10
using namespace std;
int table[MAX_N+1][MAX_N+1];
int n;

void print() {
    for(int r = 0; r < n + 1; r++) {
        for(int c = 0; c < n + 1; c++) {
            printf("%5d", table[r][c]);
        }
        putchar('\n');
    }
}

int calcVirtual(int x) {
    int sum = 0;
    for(int r = 0; r < n; r++) {
        sum += table[r][x];
    }
    return(sum);
}

int calcHorizontal(int y) {
    int sum = 0;
    for(int r = 0; r < n; r++) {
        sum += table[y][r];
    }
    return(sum);
}

int main(void) {
    while(cin >> n, n != 0) {
        for(int r = 0; r < n; r++) {
            for(int c = 0; c < n; c++) {
                cin >> table[r][c];
            }
        }
        int sum = 0;
        for(int r = 0; r < n; r++) {
            sum += (table[n][r] = calcVirtual(r));
            table[r][n] = calcHorizontal(r);
        }
        table[n][n] = sum;
        print();
    }
}

C++: #include<iostream>
#include<cstdio>
#define MAX 11
using namespace std;
int main(void){
	while(true){
		int n;
		int Mat[MAX][MAX]={{0,},};
		cin >> n;
		if( n == 0 ) break;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				cin >> Mat[i][j];
				Mat[i][n] += Mat[i][j];
				Mat[n][j] += Mat[i][j];
			}
			Mat[n][n] += Mat[i][n];
		}
		for(int i = 0; i < n+1; i++){
			for(int j = 0; j < n+1; j++){
				printf("%5d", Mat[i][j]);
			}
			putchar('\n');
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
  
using namespace std;
  
int main(void){
  
   int hyo[11][11];
   int n;
   int sum;

   while(1){
	   for(int i=0;i<10;i++){
		   for(int j=0;j<10;j++){
			   hyo[i][j]=0;
		   }
	   }
	   cin>>n;
	   if(n==0) break;
	   for(int i=0;i<n;i++){
		   for(int j=0;j<n;j++){
			   cin>>hyo[i][j];
		   }
	   }
	   for(int i=0;i<n;i++){
		   sum=0;
		   for(int j=0;j<n;j++){
			   sum+=hyo[i][j];
		   }
		   hyo[i][n]=sum;
	   }
	   for(int j=0;j<=n;j++){
		   sum=0;
		   for(int i=0;i<n;i++){
			   sum+=hyo[i][j];
		   }
		   hyo[n][j]=sum;
	   }
	   for(int i=0;i<=n;i++){
		   for(int j=0;j<=n;j++){
			   printf("%5d",hyo[i][j]);
		   }
		   printf("\n");
	   }
   }

  
   return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n;
  while(true){
  cin>>n;
  if(!n)break;
  int table[n+1][n+1];
  for(int y=0;y<n;y++){
    int s = 0;
    for(int x=0;x<n;x++){
      cin>>table[x][y];
      s+=table[x][y];
    }
    table[n][y] = s;
  }
  for(int x=0;x<n+1;x++){
    int s = 0;
    for(int y=0;y<n;y++){
      s+=table[x][y];
    }
    table[x][n] = s;
  }
  for(int y=0;y<n+1;y++){
    for(int x=0;x<n+1;x++){
      printf("%5d ",table[x][y]);
    }
    printf("\n");
  }
  }
  return 0;
}

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

int a[11][11];

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

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

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

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

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

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

int main(){
    int n;
    int *table;
    while(1){
        std::cin >> n;
        if(!n){
            return 0;
        }
        table = new int [(n + 1) * (n + 1)];
        for(int i = 0; i < n * n; i++){
            std::cin >> table[i / n * (n + 1) + i % n];
        }
        for(int r = 0; r < n; r++){
            for(int c = 0; c < n; c++){
                table[r * (n + 1) + n] += table[r * (n + 1) + c];
                table[n * (n + 1) + c] += table[r * (n + 1) + c];
                table[n * (n + 1) + n] += table[r * (n + 1) + c];
            }
        }
        
        for(int i = 0; i < (n + 1) * (n + 1); i++){
            std::cout << std::setw(5) << table[i];
            if(i % (n + 1) == n){
                std::cout << std::endl;
            }
        }
        delete [] table;
    }
}

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

#define gcu getchar_unlocked
#define ii inline int in
#define is inline bool scan
ii(int c) {
	int n = 0;
	bool minus = false; if (c == '-') minus = true, c = gcu();
	do {n = 10 * n + (c - '0'), c = gcu();} while (c >= '0');
	//return n; }
	return minus ? -n : n; }
ii() {return in(gcu());}
is(int &n) {int c=gcu();return c==EOF?false:(n=in(c),true);}
is(char &c){c=gcu();gcu();return c!=EOF;}
//is(string &s) {int c;for(;;){c=gcu();if(c=='\n'||c==' ')return true;s+=c;}}
#undef ii
#define pcu putchar_unlocked
template <typename T>
void out(T n) {
	static char buf[20];
	char *p = buf;
	if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
void out(const char *s){while(*s)pcu(*s++);}
void out(char c){pcu(c);}
//void out(string &s){for (char c: s) pcu(c);}
template <typename head, typename... tail>
void out(head&& h, tail&&... t){out(h);out(move(t)...);}
template <typename T>
//void out(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
void out(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
#undef svo

int main() {
	int n;
	while ((n = in())) {
		vector<int> v(n + 1);
		for (int i = 0; i < n; ++i) {
			int sum = 0;
			for (int j = 0; j < n; ++j) {
				int d = in();
				printf("%5d", d);
				v[j] += d;
				sum += d;
			}
			v[n] += sum;
			printf("%5d\n", sum);
		}
		for (int d: v)
			printf("%5d", d);
		out('\n');
	}
}

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

int main() {
  int n;
  while(cin >> n && n) {
    int a[n][n];
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) cin >> a[i][j];
    }
    int b[n];
    fill(b,b+n,0);
    for(int i=0; i<n; i++) {
      int sum=0;
      for(int j=0; j<n; j++) {
	printf("%5d",a[i][j]);
	sum+=a[i][j];
	b[j]+=a[i][j];
      }
      printf("%5d\n",sum);
    }
    int sum=0;
    for(int i=0; i<n; i++) {
      printf("%5d",b[i]);
      sum+=b[i];
    }
    printf("%5d\n",sum);
  }
  return 0;
}