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>

#define CEIL(a,b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))

using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'000'000'001;
constexpr ll LLINF = 4'000'000'000'000'000'001;
// constexpr int INF = 2147483647; // 2 * 1e9
// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    while(true){
        int d, w;
        cin >> d >> w;
        if(d == 0) break;
        vector<vector<int>> e(d, vector<int>(w));
        for(int i = 0; i < d; ++i){
            for(int j = 0; j < w; ++j){
                cin >> e[i][j];
            }
        }
        vector<vector<int>> a(d+1, vector<int>(w+1));
        for(int i = 1; i <= d; ++i){
            for(int j = 0; j < w; ++j){
                a[i][j+1] = a[i][j] + e[i-1][j];
            }
        }
        for(int i = 0; i < d; ++i){
            for(int j = 1; j <= w; ++j){
                a[i+1][j] += a[i][j];
            }
        }
        int ans = 0;
        for(int he = 3; he <= d; ++he){
            for(int wi = 3; wi <= w; ++wi){
                for(int y = 0; y <= d-he; ++y){
                    for(int x = 0; x <= w-wi; ++x){
                        int inner_max = 0, outer_min = INF;
                        for(int i = 0; i < he-2; ++i){
                            for(int j = 0; j < wi-2; ++j){
                                inner_max = max(inner_max, e[y + i + 1][x + j + 1]);
                            }
                        }
                        for(int i = 0; i < he; ++i){
                            if(e[i+y][x] <= inner_max) goto END;
                            if(e[i+y][x+wi-1] <= inner_max) goto END;
                            outer_min = min({outer_min, e[i+y][x], e[i+y][x+wi-1]});
                        }
                        for(int i = 0; i < wi; ++i){
                            if(e[y][x+i] <= inner_max) goto END;
                            if(e[y+he-1][x+i] <= inner_max) goto END;
                            outer_min = min({outer_min, e[y][x+i], e[y+he-1][x+i]});
                        }
                        ans = max(ans, (wi-2)*(he-2)*outer_min - (a[y+he-1][x+wi-1] - a[y+he-1][x+1] - a[y+1][x+wi-1] + a[y+1][x+1]));
                        END:;
                    }
                }
            }
        }
        cout << ans << "\n";
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

int pond[12][12];

int main(){
	int d,w;
	while(1){
		cin >> d >> w;
		if(d + w == 0) break;
		for(int i = 0;i < d;i++){
			for(int j = 0;j < w;j++){
				cin >> pond[i][j];
			}
		}
		int ans = 0;
		for(int y1 = 0;y1 < d;y1++){
			for(int y2 = y1 + 2;y2 < d;y2++){
				for(int x1 = 0;x1 < w;x1++){
					for(int x2 = x1 + 2;x2 < w;x2++){
						int minsell = pond[y1][x1];
						for(int i = x1;i <= x2;i++){
							minsell = min(minsell,min(pond[y1][i],pond[y2][i]));
						}
						for(int i = y1;i <= y2;i++){
							minsell = min(minsell,min(pond[i][x1],pond[i][x2]));
						}
						int sum = 0,flag = 0;
						for(int i = y1+1;i < y2;i++){
							for(int j = x1+1;j < x2;j++){
								if(pond[i][j] >= minsell){
									flag = 1;
								}else{
									sum += minsell - pond[i][j];
								}
							}
						}
						if(flag == 1) continue;
						ans = max(ans,sum);
					}
				}
			}
		}
		cout << ans << endl;
	}
}