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>
using namespace std;

const int INF = 1 << 30;

int grid[11][11];

void solve(int d, int w) {
    for (int i = 0; i < d; i++) {
        for (int j = 0; j < w; j++) cin >> grid[i][j];
    }
    int ans = 0;
    for (int i = 0; i < d; i++) {
        for (int j = 0; j < w; j++) {
            for (int tate = 3; tate <= d - i; tate++) {
                for (int yoko = 3; yoko <= w - j; yoko++) {
                    int min_h = INF;
                    for (int k = i; k < i + tate; k++) {
                        min_h = min(min_h, grid[k][j]);
                        min_h = min(min_h, grid[k][j + yoko - 1]);
                    }
                    for (int k = j; k < j + yoko; k++) {
                        min_h = min(min_h, grid[i][k]);
                        min_h = min(min_h, grid[i + tate - 1][k]);
                    }
                    bool is_ok = true;
                    int now_ans = 0;
                    for (int k = i + 1; k < i + tate - 1; k++) {
                        for (int l = j + 1; l < j + yoko - 1; l++) {
                            if (min_h <= grid[k][l]) {
                                is_ok = false;
                                break;
                            }
                            now_ans += min_h - grid[k][l];
                        }
                        if (!is_ok) break;
                    }
                    if (!is_ok) continue;
                    ans = max(ans, now_ans);
                }
            }
        }
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int D, W;
    while (true) {
        cin >> D >> W;
        if (D == 0 && W == 0) break;
        solve(D, W);
    }
    return 0;
}

C++: 
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

const int N = 10;

int a[N + 2][N + 2], n, m, used[N + 2][N + 2];

int in(int x, int y)
{
	if (x < 0 || x >= n || y < 0 || y >= m)
		return 0;
	return 1;
}


int test(int x, int y, int k)
{
	if (a[x][y] >= k)
		return 0;
	memset(used, 0, sizeof(used));
	queue<vector<int>> Q;
	int sum = k - a[x][y];
	used[x][y] = 1;
	int l = y, r = y, u = x, d = x;
	int cnt = 1;
	for (Q.push({ x, y }); !Q.empty(); Q.pop())
	{
		auto top = Q.front();
		for (int i = top[0] - 1; i <= top[0] + 1; i++)
			for (int j = top[1] - 1; j <= top[1] + 1; j++)
			{
				if (!in(i, j))
					continue;
				if (used[i][j])
					continue;
				if (a[i][j] >= k)
					continue;
				if (i == 0 || i == n - 1 || j == 0 || j == m - 1)
					return 0;
				//printf("%d %d %d %d\n", i, j, k, a[i][j]);
				l = min(l, j);
				r = max(r, j);
				u = min(u, i);
				d = max(d, i);
				used[i][j] = 1;
				cnt++;
				sum += k - a[i][j];
				Q.push({ i, j });
			}
	}
	if (cnt != (r - l + 1)*(d - u + 1))
		return 0;
	return sum;
}

int main()
{
	for (; scanf("%d%d", &n, &m) && (n || m); )
	{
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				scanf("%d", &a[i][j]);
		int ans = 0;
		for (int i = 1; i < n-1; i++)
			for (int j = 1; j < m-1; j++)
				for (int k = 1; k <= 9; k++)
				{
					ans = max(ans, test(i, j, k));
					//printf("i=%d j=%d k=%d   %d\n", i, j, k, test(i, j, k));
				}
		printf("%d\n", ans);
	}
}

/*


*/