// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
using namespace std;
// mylittledoge
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N,M;
cin >> N >> M;
vector< vector<int> > V(N,vector<int>(M));
for(int i =0; i < N; i++) for(int j =0; j < M; j++) cin >> V[i][j];
vector<int> H(M,0);
long long ans =0;
for(int i =0; i < N; i++) {
stack< pair<int,int> > S;
S.push(make_pair(0,-1));
for(int j =0; j < M; j++) {
H[j]++;
if(i > 0 && V[i][j] != V[i-1][j]) H[j] =1;
int q =j;
if(j > 0 && V[i][j] != V[i][j-1]) {
while(S.size() > 1) {
pair<int,int> p =S.top(); S.pop();
ans +=1LL*(j-p.ss)*(j-p.ss+1)/2*1LL*(p.ff-S.top().ff);}
S.pop();
S.push(make_pair(0,j-1));}
while(S.top().ff > H[j]) {
pair<int,int> p =S.top(); S.pop();
q =p.ss;
ans +=1LL*(j-p.ss)*(j-p.ss+1)/2*1LL*(p.ff-max(H[j],S.top().ff));}
if(S.top().ff != H[j]) S.push(make_pair(H[j],q));}
while(S.top().ff > 0) {
pair<int,int> p =S.top(); S.pop();
ans +=1LL*(M-p.ss)*(M-p.ss+1)/2*1LL*(p.ff-S.top().ff);}
}
cout << ans << "\n";
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZG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