#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define All(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()   //reverse
#define strall(v) (v).cbegin(),(v).cend() //const_itterator
#define IN(a, b, x) (a<=x&&x<b)
using namespace std;
using ll = long long;
using Pair = pair<int,int>;
using Graph = vector<vector<ll>>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<typename t, typename u, typename Comp=less<>>
bool chmax(t& xmax, const u& x, Comp comp={}) { if(comp(xmax, x)) { xmax = x; return true; } return false;}
template<typename t, typename u, typename Comp=less<>>
bool chmin(t& xmin, const u& x, Comp comp={}) { if(comp(x, xmin)) { xmin = x; return true;} return false;}
const int INF = 1e9;
const ll infl = ll(1e18)+5;

int main(){
    int H,W;cin >> H >> W;
    int min_block = 105;
    vector<vector<int>> V(H,vector<int>(W,0));
    rep(i,H){
        rep(j,W){
            
            cin >> V[i][j];
            min_block = min(min_block,V[i][j]);
        }
    }
    int ans = 0;
    rep(i,H){
        rep(j,W){
           ans += V[i][j]-min_block; 
        }
    }
    cout << ans << endl;
}
#include<iostream>
using namespace std;

int main() {
	int n;
	cin>>n;
	string str;
	cin>>str;
	
	for (int i=n-1;i>=0;i--) {
		if (i==n-1 && str[0] != str[i]) {
			cout<<1<<endl;
			return 0;
		}
		else if (i<n-2 && str[0] != str[i] && str[n-1] != str[i+1]) {
			
			cout<<2<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}






