#include <bits/stdc++.h>
#include <limits.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
using namespace std;
template<typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define lsb(x) (__builtin_ctzll(x)) //pos of least set bit (first 1)
#define msb(x) (63 - __builtin_clzll(x)) //pos of most set bit (last 1)
ll dx[8] = {-1,0,1,0,-1,-1,1,1};
ll dy[8] = {0,1,0,-1,-1,1,1,-1};
void fileIO(void) {

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

#endif

}
void fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  }   

  ll calc(vector<ll> v){
    sort(v.begin(),v.end());
    ll x=0, pref=0;
    for(ll i=0; i<v.size(); i++){
      x += i*v[i] - pref;
      pref+=v[i];
    }
    return x;
  }

  void solve(){
ll n,m; cin>>n>>m;
const ll maxc=1e5;
vector<vector<ll>> col_rows(maxc+1) , col_columns(maxc+1);

for(ll i=0; i<n; i++){
  for(ll j=0; j<m; j++){
    ll color; cin>>color;
    col_rows[color].push_back(i);
    col_columns[color].push_back(j);
  }
}
ll ans=0;
for(ll i=1; i<=maxc; i++){
  ans += calc(col_rows[i]);
  ans += calc(col_columns[i]);
}
cout<<ans;
}

  int main() {
    fileIO();
    fastIO();
    ll testcases=1;
    //cin>>testcases;
    while(testcases--){
        solve();
    }

}