Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		d, w := io.NextInt(), io.NextInt()
		if d == 0 {
			break
		}
		e := make([][]int, d)
		for i := 0; i < d; i++ {
			e[i] = make([]int, w)
			for j := 0; j < w; j++ {
				e[i][j] = io.NextInt()
			}
		}
		ans := 0
		for i := 0; i < d-2; i++ {
			for j := 0; j < w-2; j++ {
				for k := i + 2; k < d; k++ {
					for l := j + 2; l < w; l++ {
						ans = intMax(vol(e, i, j, k, l), ans)
					}
				}
			}
		}
		io.Println(ans)
	}
}

func vol(e [][]int, a, b, c, d int) int {
	outer := 10
	for i := a; i <= c; i++ {
		outer = intMin(e[i][b], outer)
		outer = intMin(e[i][d], outer)
	}
	for j := b; j <= d; j++ {
		outer = intMin(e[a][j], outer)
		outer = intMin(e[c][j], outer)
	}
	a++
	b++
	vol := 0
	for i := a; i < c; i++ {
		for j := b; j < d; j++ {
			if e[i][j] >= outer {
				return 0
			}
			vol += outer - e[i][j]
		}
	}
	return vol
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func intMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<iostream>

int garden[10][10] = {};
int min = 10;

int pond_area(int sx,int sy,int width,int height){
    int area = 0;
    for(int i=sx+1;i<width+sx-1;i++){
        for(int j=sy+1;j<height+sy-1;j++){
            int d = garden[j][i];
            area += (min - d);
        }
    }
    return area;
}
    
bool isPond(int sx,int sy,int width,int height){
    //std::cout << "start:(" << sx << ',' << sy << ") width = " << width << " height=" << height << '\n';
    int max = 0;
    min = 10;
    for(int k=sy;k<height+sy;k++){
        int d1 = garden[k][sx];
        int d2 = garden[k][sx+width-1];
        if(d1>d2) d1 = d2;
        if(min>d1) min = d1;
    }
    for(int k=sx;k<width+sx;k++){
        int d1 = garden[sy][k];
        int d2 = garden[sy+height-1][k];
        if(d1>d2) d1=d2;
        if(min>d1) min = d1;
    }
    
    for(int i=sx+1;i<width+sx-1;i++){
        for(int j=sy+1;j<height+sy-1;j++){
            int d = garden[j][i];
            if(max<d) max = d;
        }
    }
    
    //std::cout << "  min=" << min << "  max=" << max << '\n';
    
    if(min>max) return true;
    else return false;
}

    
int main(){
    int m,n;
    while(1){
        int max = 0;
        std::cin >> m >> n;
        if(m==0&&n==0) break;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                std::cin >> garden[i][j];
            }
        }
        /*for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                std::cout << garden[i][j] << ' ';
            }
            std::cout << '\n';
        }
         */
        for(int height=m;height>2;height--){
            for(int width=n;width>2;width--){
                for(int sy=0;sy<m-height+1;sy++){
                    for(int sx=0;sx<n-width+1;sx++){
                        if(isPond(sx,sy,width,height)){
                            int area = pond_area(sx,sy,width,height);
                            if(area > max) max = area;
                            //std::cout << "area=" << area << '\n';
                        }
                    }
                }
            }
        }
        std::cout << max << '\n';
    }
    return 0;
}

C++: #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=998244353;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
//  10:00-
//A 10:05 AC
//B 10:15 AC
int solve(void){
	int d,w,ya,xa,yb,xb,i,j,ans=0;cin>>d>>w;
	if(d==0){return 0;}
	int ta[20][20];
	for(i=0;i<d;i++){
		for(j=0;j<w;j++){
			cin>>ta[i][j];
		}
	}
	for(ya=0;ya<d;ya++){
		for(xa=0;xa<w;xa++){
			for(yb=ya+2;yb<d;yb++){
				for(xb=xa+2;xb<w;xb++){
					int soto=mod;
					for(i=ya;i<=yb;i++){mineq(soto,ta[i][xa]);mineq(soto,ta[i][xb]);}
					for(j=xa;j<=xb;j++){mineq(soto,ta[ya][j]);mineq(soto,ta[yb][j]);}
					int gen=0;
					for(i=ya+1;i<=yb-1;i++){
						for(j=xa+1;j<=xb-1;j++){
							if(ta[i][j]>=soto){gen=-mod;break;}
							gen+=soto-ta[i][j];
						}
					}
					maxeq(ans,gen);
				}
			}
		}
	}
	cout<<ans<<endl;
	return 1;
}
int main(void){
	while(solve()){}
}