#include <stdio.h>
int main() {
int a[20][40],n,m,s=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
for(int p=1;p<=n*m;p++){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]!=-1){
int l=1,r=1,u=1,d=1;
while(a[i][j+r]==-1 && j+r<m){
r=r+1;
}
while(a[i][j-l]==-1 && j-l>0){
l=l+1;
}
while(a[i-u][j]==-1 && i-u>0){
u=u+1;
}
while(a[i+d][j]==-1 && i+d<n){
d=d+1;
}
if(a[i][j]==a[i][j+r] && j+r<m){
s=s+a[i][j];
a[i][j]=-1;
a[i][j+r]=-1;
continue;
}
if(a[i][j]==a[i][j-l] && j-l>0){
s=s+a[i][j];
a[i][j]=-1;
a[i][j-l]=-1;
continue;
}
if(a[i][j]==a[i-u][j] && i-u>0){
s=s+a[i][j];
a[i][j]=-1;
a[i-u][j]=-1;
continue;
}
if(a[i][j]==a[i+d][j] && i+d<n){
s=s+a[i][j];
a[i][j]=-1;
a[i+d][j]=-1;
continue;
}
}
}
}
}
printf("%d",s);
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
s:總和
r:向右比對
l:向左比對
u:向上比對
d:向下比對