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>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <math.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const ll mod = 1000000007;
int d, w, e[11][11], ans;

map<P, int> mp;

ll pow(ll x, ll y)
{
	if (y == 0) return 1;
	if (y % 2)
	{
		return pow(x, y - 1) * x % mod;
	}
	else
	{
		ll yyy = pow(x, y / 2);
		return yyy * yyy % mod;
	}
}

ll div(ll x)
{
	return pow(x, mod - 2);
}

ll gcd(ll x, ll y)
{
	if (y == 0)
	{
		return x;
	}
	else
	{
		return gcd(y, x % y);
	}
}

int main(void)
{
	while (true)
	{
		cin >> d >> w;
		if (d == 0 && w == 0) break;
		for (int i = 1; i <= d; i++)
		{
			for (int j = 1; j <= w; j++)
			{
				cin >> e[i][j];
			}
		}
		ans = 0;
		for (int i = 1; i <= d - 2; i++)
		{
			for (int j = i + 2; j <= d; j++)
			{
				for (int k = 1; k <= w - 2; k++)
				{
					for (int l = k + 2; l <= w; l++)
					{
						int hei = 20;
						bool jud = true;
						int wat = 0;
						for (int m = k; m <= l; m++)
						{
							hei = min(hei, e[i][m]);
							hei = min(hei, e[j][m]);
						}
						for (int m = i; m <= j; m++)
						{
							hei = min(hei, e[m][k]);
							hei = min(hei, e[m][l]);
						}
						for (int m = i + 1; m < j; m++)
						{
							for (int o = k + 1; o < l; o++)
							{
								if (hei <= e[m][o]) jud = false;
								wat += hei - e[m][o];
							}
						}
						if (jud)
						{
							ans = max(ans, wat);
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	int www;
	cin >> www;
}

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

int e[15][15];
int calc(int y1, int y2, int x1, int x2) {
    int outer = 1e9;
    // 壁の高さの最小値
    for (int i = y1; i <= y2; i++) {
        for (int j = x1; j <= x2; j++) {
            if (i == y1 || i == y2 || j == x1 || j == x2) {
                outer = min(outer, e[i][j]);
            }
        }
    }
    int res = 0;
    for (int i = y1; i <= y2; i++) {
        for (int j = x1; j <= x2; j++) {
            if (i == y1 || i == y2 || j == x1 || j == x2) {
                continue;
            }
            if (e[i][j] >= outer) return 0;
            res += outer - e[i][j];
        }
    }
    return res;
}

int main() {
    int d, w;
    while (cin >> d >> w, d | w) {
        for (int i = 0; i < d; i++) {
            for (int j = 0; j < w; j++) {
                cin >> e[i][j];
            }
        }
        int ans = -1;
        for (int y1 = 0; y1 < d; y1++) {
            for (int x1 = 0; x1 < w; x1++) {
                for (int y2 = y1 + 2; y2 < d; y2++) {
                    for (int x2 = x1 + 2; x2 < w; x2++) {
                        ans = max(ans, calc(y1, y2, x1, x2));
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}