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>
using namespace std;

int a[11][11];

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

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

    for (i = 0; i <= n; i++){
      cout << a[i][0];
      for (j = 1; j <= n; j++){
        cout << " " << a[i][j];
      }
      cout << endl;
    }
    return 0;
  }
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;
const int MAX = 101;

template<class T, class U>
void convert(T &t, U &u){
	stringstream ss;
	ss << t;
	ss >> u;
}

int main(){
	int n;
	while(cin >> n && n != 0){
		vector<vector<int> > ans(n+1, vector<int>(n+1, 0));
	
		for(int i = 0; i < n; i++){
			int total = 0;
			for(int j = 0; j < n; j++){
				cin >> ans[i][j];
				total += ans[i][j];
				ans[n][j] += ans[i][j];
			}
			ans[i][n] = total;
			ans[n][n] += total;
		}

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

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

int main() {
	int N;

	while (cin >> N && N != 0) {
		int dataset[N+1][N+1];
        for (int i = 0; i != N; ++i) {
            for (int j = 0; j != N; ++j) {
                cin >> dataset[i][j];
            }
        }

        for (int i = 0; i != N; ++i) {
            int total = 0;
            for (int j = 0; j != N; ++j) {
                total += dataset[i][j];
            }
            dataset[i][4] = total;
        }

        for (int i = 0; i != N; ++i) {
            int total = 0;
            for (int j = 0; j != N; ++j) {
                total += dataset[j][i];
            }
            dataset[4][i] = total;
        }

        int ans = 0;
        for (int i = 0; i != N; ++i) {
            ans += dataset[4][i];      
        }
        dataset[4][4] = ans;

        for (int i = 0; i != N+1; ++i) {
            for (int j = 0; j != N; ++j) {
                cout << setw(5) << dataset[i][j];
            } 
            cout << setw(5) << dataset[i][4] << endl;
        }
	}
    return 0;
}

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

using namespace std;

int main(){
  int n;
  while(cin>>n&&n){
    vector<int> data((n+1)*(n+1));
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        cin>>data[i*(n+1)+j];
        data[i*(n+1)+n]+=data[i*(n+1)+j];
        data[n*(n+1)+j]+=data[i*(n+1)+j];
        data[(n+1)*(n+1)-1]+=data[i*(n+1)+j];
      }
    }
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
        cout<<setw(5)<<data[i*(n+1)+j];
      }
      cout<<endl;
    }
  }
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int main() {
	int n;
	while (cin >> n&&n) {
		int g[15][15];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> g[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			int sum = 0;
			for (int j = 0; j < n; j++) {
				sum += g[i][j];
			}
			g[i][n] = sum;
		}
		for (int j = 0; j < n; j++) {
			int sum = 0;
			for (int i = 0; i < n; i++) {
				sum += g[i][j];
			}
			g[n][j] = sum;
		}
		int ssum = 0;
		for (int i = 0; i < n; i++) {
			ssum += g[i][n];
		}
		g[n][n] = ssum;
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				if (g[i][j] < 10)cout << "    " << g[i][j];
				else if (g[i][j] < 100)cout << "   " << g[i][j];
				else if (g[i][j] < 1000)cout << "  " << g[i][j];
				else if (g[i][j] < 10000)cout << " " << g[i][j];
			}
			cout << endl;
		}
	}
}

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

int main(int argc, char *argv[]){
  int n;
  cin >> n;
  int mat[n + 1][n + 1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> mat[i][j];
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int hor = 0;
    int var = 0;
    for (int j = 0; j < n; j++) {
      hor += mat[i][j];
      var += mat[j][i];
    }
    mat[i][n] = hor;
    mat[n][i] = var;
    sum += hor;
  }
  mat[n][n] = sum;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      printf("%5d ", mat[i][j]);
    }
    std::cout << std::endl;
  }
  return 0;
}

C++: #include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <memory>
#include <iomanip>

int main(int argc, char* argv[])
{
    while (true) {
        std::string line;
        std::stringstream buffer;
        getline(std::cin, line);
        buffer << line;
        size_t length = 0;
        buffer >> length;
        if (length == 0) break;
        int *total = new int[length + 1];
        size_t width = length + 1;
        memset(total, 0, sizeof(int) * width);
        for (size_t i = 0; i < length; ++i) {
            getline(std::cin, line);
            std::istringstream s(line);
            std::string element;
            int sum = 0;
            for (size_t n = 0; n < length; ++n) {
               getline(s, element, ' ');
               std::stringstream tmp;
                int val = 0;
                tmp << element;
                tmp >> val;
                sum += val;
                total[n] += val;
                std::cout << std::setw(5) << val;
            }
            std::cout << std::setw(5) << sum << std::endl;
            total[length] += sum;
        }
        for (size_t i = 0; i < width; ++i) {
            std::cout << std::setw(5) << total[i];
        }
        std::cout << std::endl;
        delete[] total;
    }
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(void) {
  int n;
  int data[100][100];

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

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