#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <ctype.h>

using namespace std;

int main(){

    ifstream in ("DATA5.txt");
    ofstream out ("OUT5.txt");
    
    int m, n;
    int city[20][20];
    int testcity[20][20];
    int maxvalue=0;
    bool jesse;
    int temp;
    int counter=0;
    
    
    for (int i=0; i<5; i++){
        jesse=1;
        counter=0;
        in >> n >> m;
        
        for (int j=0; j<n; j++){
            for (int k=0; k<m; k++){
                in >> city[j][k];
                testcity[j][k]=city[j][k];
                maxvalue=max(maxvalue, city[j][k]);
            }
        }
        
        for (int j=1; j<n-1; j++){
            for (int k=1; k<m-1; k++){
                testcity[j][k] = maxvalue;
            }
        }    
        
        while (jesse){
           jesse=false;
           for (int j=1; j<n-1; j++){
               for (int k=1; k<m-1; k++){
                   temp=max(city[j][k], min(min(testcity[j+1][k], testcity[j-1][k]), min(testcity[j][k+1], testcity[j][k-1])));
                   if (temp!=testcity[j][k])
                      jesse=1;
                   testcity[j][k]=temp;
                   
               }
           }
        }
           
        for (int j=1; j<n-1; j++){
            for (int k=1; k<m-1; k++){
                counter+=testcity[j][k]-city[j][k];
            }
        }    
            
            
        out << counter << endl;
            
//        for (int j=0; j<n; j++){
//            for (int k=0; k<m; k++)
//                out << testcity[j][k];
//            out << endl;
//        }

  // bailey is a goose jesse is a duck jason is a whale and jenn is a fish      
    }


    in.close();
    out.close();

    return 0;
}

