#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
int card[20][40];
scanf("%d %d", &n, &m);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++) scanf("%d", &card[i][j]);
}
int sum=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(card[i][j]==-1) continue;
//檢查向上
int up=i-1;
while(up>=0 && card[up][j]==-1) up--;
if(up>=0 && card[up][j]==card[i][j])
{
sum+=card[i][j];
card[up][j]=-1;
card[i][j]=-1;
continue;
}
//檢查向下
int down=i+1;
while(down<n && card[down][j]==-1) down++;
if(down<n && card[down][j]==card[i][j])
{
sum+=card[i][j];
card[down][j]=-1;
card[i][j]=-1;
continue;
}
//檢查向左
int left=j-1;
while(left>=0 && card[i][left]==-1) left--;
if(left>=0 && card[i][left]==card[i][j])
{
sum+=card[i][j];
card[i][left]=-1;
card[i][j]=-1;
continue;
}
//檢查向右
int right=j+1;
while(right<m && card[i][right]==-1) right++;
if(right<m && card[i][right]==card[i][j])
{
sum+=card[i][j];
card[i][right]=-1;
card[i][j]=-1;
continue;
}
}
}
printf("%d", sum);
}