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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, l, r) for (int i = (l); i < (r); i++)

int main() {
    int h, w;
    while (cin >> h >> w, h) {
        vector<vector<int>> field(h, vector<int>(w));
        rep(i, 0, h) rep(j, 0, w) cin >> field[i][j];
        int ans = 0;
        rep(top, 0, h) rep(bottom, top + 2, h)      // [top, bottom]
        rep(left, 0, w) rep(right, left + 2, w) {   // [left, right]
            int edge_min = 10, pond_max = -1, pond_sum = 0, pond_cnt = 0;
            rep(i, top, bottom + 1) rep(j, left, right + 1) {
                bool is_edge = i == top || i == bottom;
                is_edge |= j == left || j == right;
                if (is_edge) {
                    edge_min = min(edge_min, field[i][j]);
                } else {
                    pond_max = max(pond_max, field[i][j]);
                    pond_sum += field[i][j];
                    pond_cnt += 1;
                }
            }
            if (edge_min > pond_max) {
                ans = max(ans, edge_min * pond_cnt - pond_sum);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main(){
    while(1){
        int d, w;
        cin >> d >> w;
        if (d == 0) break;
        vector<vector<int>> pond(d, vector<int>(w));
        for (int i = 0; i < d; i++){
            for (int j = 0; j < w; j++){
                cin >> pond[i][j];
            }
        }
        int ans = 0;
        int pond_min, pond_max;
        int tmp;
        for (int i = 3; i <= d; i++){
            for (int j = 3; j <= w; j++){
                for (int s = 0; s <= d - i; s++){
                    for (int t = 0; t <= w - j; t++){
                        tmp = 0;
                        pond_min = 10;
                        pond_max = 0;
                        for (int x = 0; x < i; x++){
                            for (int y = 0; y < j; y++){
                                if (x == 0 || y == 0 || x == i - 1 || y == j - 1) pond_min = min(pond_min, pond[s+x][t+y]);
                                else {
                                    tmp += pond[s+x][t+y];
                                    pond_max = max(pond_max, pond[s+x][t+y]);
                                }
                            }
                        }
                        if (pond_max < pond_min) ans = max(ans, pond_min * (i - 2) * (j - 2) - tmp);
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include "bits/stdc++.h"

#define REP(i, n ,N) for(ll i = (n); i < (N); i++)
#define p(s) cout<<(s)<<endl
#define p2(a, b) cout<<(a)<<" "<<(b)<<endl
using namespace std;
typedef long long ll;
ll MOD = 1e9+7;
ll inf = 1e18;

ll d, w;
ll field[11][11];
int main() {
	while (cin >> d >> w && d > 0) {
		REP(i, 0, d) {
			REP(j, 0, w) {
				cin >> field[i][j];
			}
		}
		ll ans = 0;
		REP(y1, 0, d) REP(x1, 0, w) REP(y2, y1 + 2, d) REP(x2, x1 + 2, w) {
			ll mx = inf;
			REP(y, y1, y2 + 1) {
				mx = min(mx, field[y][x1]);
				mx = min(mx, field[y][x2]);
			}
			REP(x, x1, x2 + 1) {
				mx = min(mx, field[y1][x]);
				mx = min(mx, field[y2][x]);
			}
			ll sum = 0;
			bool ok = true;
			REP(y, y1 + 1, y2) {
				REP(x, x1 + 1, x2) {
					if (mx - field[y][x] <= 0) {
						ok = false;
						break;
					}
					sum += mx - field[y][x];
				}
				if (!ok) break;
			}
			if (ok) ans = max(ans, sum);
		}
		p(ans);
	}

	return 0;
}