#include <bits/stdc++.h>
using namespace std;
string s;
int n,m,b[10000][10000],a[10000][10000],m1,n1,vitri1,vitri2,vitri3,vitri4,gtln=INT_MIN;

int main()
{
    freopen("matran.inp","r",stdin);
    freopen("matran.out","w",stdout);
    cin>>m>>n;
    for (int i=1;i<m+1;i++)
    {
        for (int j=1;j<n+1;j++)
        {
            cin>>a[i][j];
            b[i][j]=a[i][j]+b[i-1][j]+b[i][j-1]-b[i-1][j-1];
        }
    }
    for (int m1=1;m1<=m;m1++)
    {for (int n1=1;n1<=n;n1++)
    for (int i=m1;i<m+1;i++)
    {
        for (int j=n1;j<n+1;j++)
        {
            if (gtln<b[i][j]-b[i-m1][j]-b[i][j-n1]+b[i-m1][j-n1])
            {
                gtln=b[i][j]-b[i-m1][j]-b[i][j-n1]+b[i-m1][j-n1];
                vitri1=i-m1+1;
                vitri2=j-n1+1;
                vitri3=i;
                vitri4=j;
            }
        }
    }
    }
    for (int i=vitri1;i<=vitri3;i++)
    {
        for (int j=vitri2;j<=vitri4;j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    for (int i=1;i<m+1;i++)
    {
        int gtln=INT_MIN;
        for (int j=1;j<n+1;j++)
        {
            gtln=max(gtln,a[i][j]);
        }
        cout<<gtln<<endl;
    }
}

