#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define ull unsigned long long
#define db double
#define ull unsigned long long
#define MIN INT_MIN
#define MAX INT_MAX
#define Sort(x) sort((x).begin(), (x).end())
#define pb push_back
#define ii pair <int,int>
#define iii pair <int,ii>
#define pll pair <ll,ll>
#define plll pair <ll,pll>
#define ff first
#define ss second
#define minQueue priority_queue <int,vector<int>,greater<int> >
#define maxQueue priority_queue<int,vector<int>,less<int> >
#define pb push_back
#define max3(a,b,c) max(a, max(b,c))
#define min3(a,b,c) min(a, min(b,c))
#define GCD(x,y) __gcd((x), (y))
#define LCM(x,y) ((x / GCD((x), (y))) * y)
#define loop(a,n) for(int i=a; i<n; i++)
#define mem(x,y) memset((x), (y), sizeof (x));
#define Case int T; scanf("%d", &T); for(int cas = 1; cas <= T; ++cas)
#define pf printf
#define sf scanf
#define pr1(x) cout << x << endl
#define pr2(x,y) cout << x << ' ' << y << endl
#define powerOfTwo(x) (x && !(x & (x - 1)))
#define Iterator(type) type <int> :: iterator
#define mapIterator map <int,int> :: iterator //it->ff it->ss
#define FI freopen ("input.txt", "r", stdin)
#define FO freopen ("output.txt", "w", stdout)
#define Ziru 0
using namespace std;
/*int intConvert(string s){
    int n;
    stringstream geek(s);
    geek >> n;
    return n;
}*/

/*string strConvert(int n){
    string s;
    stringstream ss;
    ss << n;
    s=ss.str();
    return s;
}*/

///***********BigMod************************************************************************************
//int StringMod(string num, int a){
//    int res = 0;
//    for (int i = 0; i < num.length(); i++)
//         res = (res*10 + (int)num[i] - '0') %a;
//    return res;
//}

//int BigMod(ll b,ll p, ll m){
//   if(p==0)return 1;
//   else if(p%2==1){
//        int part1=b%m;
//        int part2=BigMod(b,p-1,m);
//        return (part1*part2)%m;
//
//   }
//   else if(p%2==0){
//        int half=BigMod(b,p/2,m);
//        return (half*half)%m;
//   }
//}
///BigMod (89, 14 ,2589)==1849
///StringMod(1956410986640441413344189841, 2589)==1849;


///_________________factorial___________________________________________________________________________

//int multiply(int x, int res[], int res_size);
//
//void factorial(int n){
//    int res[500];
//    res[0] = 1;
//    int res_size = 1;
//    for (int x=2; x<=n; x++)
//        res_size = multiply(x, res, res_size);
//    cout << "Factorial of given number is \n";
//    for (int i=res_size-1; i>=0; i--)
//        cout << res[i];
//}
//int multiply(int x, int res[], int res_size){
//    int carry = 0;
//    for (int i=0; i<res_size; i++){
//        int prod = res[i] * x + carry;
//        res[i] = prod % 10;
//        carry  = prod/10;
//    }
//    while (carry){
//        res[res_size] = carry%10;
//        carry = carry/10;
//        res_size++;
//    }
//    return res_size;
//}


int main(){
    int n,m,sum=0;
    vector <int> v;
    cin>>n>>m;
    int arr[n][m];

    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>arr[i][j];
            sum+=arr[i][j];
        }
        v.push_back(sum);
        sum=0;
    }

    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cout<<arr[i][j]<<" ";
        }
        cout<<v[i]<<endl;
    }

    int sum1=0,sum2=0;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            sum1+=arr[j][i];
        }
        sum2+=sum1;
        cout<<sum1<<" ";
        sum1=0;
    }
    cout<<sum2<<endl;
	return Ziru;
}



Measured memory_bytes: 3176