#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=110;
const int inf=0x3f3f3f3f;
int n,m,ans,match[N],w[N][N],d;
int fx[N],fy[N],lx[N],ly[N];
bool dfs(int u){
fx[u]=1;
for(int i=1;i<=m;i++){
if(!fy[i]&&lx[u]+ly[i]==w[u][i]){
fy[i]=1;
if(!match[i]||dfs(match[i])){
match[i]=u;
return 1;
}
}
}
return 0;
}
void KM(){
for(int i=1;i<=n;i++){
lx[i]=-inf;
for(int j=1;j<=m;j++) lx[i]=max(lx[i],w[i][j]);
}
for(int i=1;i<=n;i++){
for(;;){
memset(fx,0,sizeof fx);
memset(fy,0,sizeof fy);
if(dfs(i)) break;
d=inf;
for(int j=1;j<=n;j++){
for(int k=1;k<=m;k++){
if(fx[j]&&!fy[k]){
d=min(d,lx[j]+ly[k]-w[j][k]);
}
}
}
if(d==inf) break;
for(int j=1;j<=n;j++) if(fx[j])lx[j]-=d;
for(int j=1;j<=m;j++) if(fy[j])ly[j]+=d;
}
}
for(int i=1;i<=m;i++) if(match[i]) ans+=w[match[i]][i];
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&w[i][j]);
}
}
KM();
printf("%d",ans);
return 0;
}