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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

template<class Type>
Type solve(Type res = Type()){
    ll N; cin >> N;
    vector<ll> a(N); for(auto& in:a) cin >> in;
    res = accumulate(a.begin(),a.end(),0LL) - N;
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    // solve(0);
    // cout << fixed << setprecision(15) << solve<ll>() << endl;
    
    int d,w;
    while(cin >> d >> w,d){
        auto e = make_v<int>(d,w);
        for(auto& vec:e) for(auto& in:vec) cin >> in;

        int ans = 0;
        for(int uly = 0; uly < d; uly++){
            for(int ulx = 0; ulx < w; ulx++){
                for(int lry = uly+2; lry < d; lry++){
                    for(int lrx = ulx+2; lrx < w; lrx++){
                        int minwall = INF;
                        for(int z = uly; z <= lry; z++) minwall = min(minwall,e[z][ulx]);
                        for(int z = uly; z <= lry; z++) minwall = min(minwall,e[z][lrx]);
                        for(int z = ulx; z <= lrx; z++) minwall = min(minwall,e[uly][z]);
                        for(int z = ulx; z <= lrx; z++) minwall = min(minwall,e[lry][z]);
                        
                        int T = 0;
                        for(int iny = uly+1; iny < lry; iny++){
                            for(int inx = ulx+1; inx < lrx; inx++){
                                if(minwall <= e[iny][inx]) T = -INF;
                                T += minwall - e[iny][inx];
                            }
                        }
                        ans = max(ans,T);
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: 
#include"bits/stdc++.h"
using namespace std;

#define REP(i,N) for(int i=0;i<(N);i++)
#define FOR(i,M,N) for(int i=M;i<(N);i++)
#define unko int

const int INF = 1 << 17;

int A[11][11];
int getmin(int x,int y,int X,int Y) {
	int mn = INF;
	FOR(i, x, X + 1) {
		mn = min(mn, A[i][y]);
		mn = min(mn, A[i][Y]);
	}
	FOR(j, y, Y + 1) {
		mn = min(mn, A[x][j]);
		mn = min(mn, A[X][j]);
	}
	return mn;
}
int getmax(int x, int y, int X, int Y) {
	int mx = -INF;
	FOR(i, x + 1, X) {
		FOR(j, y + 1, Y) {
			mx = max(mx, A[i][j]);
		}
	}
	return mx;
}
int calc(int x, int y, int X, int Y, int mn) {
	int res = 0;
	FOR(i, x + 1, X) {
		FOR(j, y + 1, Y) {
			if (A[i][j] >= mn)return 0;
			res += mn - A[i][j];
		}
	}
	return res;
}
int H, W;

void solve() {
	int ans = 0;
	REP(i, H - 2) {
		REP(j, W - 2) {
			FOR(k, i + 2, H) {
				FOR(l,j + 2, W) {
					int mn = getmin(i, j, k, l);
					int mx = getmax(i, j, k, l);
					if (mn > mx) {
						int res = calc(i, j, k, l, mn);
						ans = max(ans, res);
					}
				}
			}
		}
	}
	cout << ans << endl;
}

unko main() {
	while (true) {
		 cin >> H >> W;
		if (H == 0 && W == 0) {
			return 0;
		}
		REP(i, 10) {
			REP(j, 10)A[i][j] = 0;
		}
		REP(i, H) {
			REP(j, W) {
				cin >> A[i][j];
			}
		}
		solve();
	}
}