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 rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int grid[10][10];

void init(){
    rep(i, 10) rep(j, 10) grid[i][j] = -1;
}

int main(void){
    //左上と右下を指定する
    //枠を見て最小値を決める
    //内部を見る
    int H, W;
    vector<int> ans;
    while(true){
        cin >> H >> W;
        if(H == 0) break;
        int res = 0;
        rep(i, H) rep(j, W) cin >> grid[i][j];
        for(int i = 0; i < H-2; i++){
            for(int j = 0; j < W-2; j++){
                for(int k = i+2; k < H; k++){
                    for(int l = j+2; l < W; l++){
                        //grid[i][j]が左上、grid[k][l]が右下
                        int temp = 0;
                        int base_height = mod;
                        vector<int> naibu;
                        for(int i2 = i; i2 <= k; i2++){
                            for(int j2 = j; j2 <= l; j2++){
                                if(i2 == i || i2 == k || j2 == j || j2 == l) base_height = min(base_height, grid[i2][j2]);
                                else naibu.push_back(grid[i2][j2]);
                            }
                        }
                        rep(a, naibu.size()){
                            if(naibu[a] >= base_height) temp = -mod;
                            else temp += base_height - naibu[a];
                        }
                        res = max(res, temp);
                        //if(i == 0 && j == 0 && k == 2 && l == 3) cout << base_height << " " << naibu.size() << " " << res << endl;
                    }
                }
            }
        }
        ans.push_back(res);
    }
    rep(i, ans.size()) cout << ans[i] << endl;
    return 0;
}

C++: #include <bits/stdc++.h>

class Solve {
private:
    using vi = std::vector<int>;
    using vvi = std::vector<vi>;
    using vb = std::vector<bool>;
    using vvb = std::vector<vb>;

    int D, W;
    vvi height;
    vvb visited;
    int calcMin(int t, int b, int l, int r)
    {
        int min{100};
        for (int i{t - 1}; i <= b + 1; i++)
            min = std::min({min, height[i][l - 1], height[i][r + 1]});
        for (int i{l - 1}; i <= r + 1; i++)
            min = std::min({min, height[t - 1][i], height[b + 1][i]});
        return min;
    }

    int calcCap(int t, int b, int l, int r, int h)
    {
        int ret{};
        for (int i{t}; i <= b; i++)
            for (int j{l}; j <= r; j++)
            {
                if (height[i][j] >= h) return -1;
                ret += h - height[i][j];
            }
        return ret;
    }
public:
    bool is_last_{};
    Solve()
    {
        scanf("%d%d", &D, &W);
        if (D == 0 && W == 0)
        {
            is_last_ = true;
            return;
        }
        height.resize(D + 2, vi(W + 2, -1));
        for (int i{1}; i <= D; i++)
            for (int j{1}; j <= W; j++)
                scanf("%d", &height[i][j]);
        int max{};
        for (int t{2}; t < D; t++)
            for (int b{t}; b < D; b++)
                for (int l{2}; l < W; l++)
                    for (int r{l}; r < W; r++)
                    {
                        int h{calcMin(t, b, l, r)};
                        max = std::max(max, calcCap(t, b, l, r, h));
                    }
        printf("%d\n", max);
    }
};

int main()
{
    while (!Solve().is_last_);

    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int main(){
	while(1){
		int d, w; cin >>d>>w;
		if(!d && !w) break;
		int e[10][10];
		for(int y=0; y<d; y++){
			for(int x=0; x<w; x++){
				cin >> e[y][x];
			}
		}

		int ans=0;
		for(int i=0; i<d; i++){
			for(int j=0; j<w; j++){
				for(int k=0; k<d; k++){
					for(int l=0; l<w; l++){
						int low=10, high=0, tmp=0;
						
						for(int m=i; m<=k; m++){
							for(int n=j; n<=l; n++){
								if(m==i || m==k || n==j || n==l){
									if(e[m][n] < low) low = e[m][n];
								}
							}
						}
						
						for(int m=i+1; m<k; m++){
							for(int n=j+1; n<l; n++){
								high = max(e[m][n], high);
								tmp += low - e[m][n];
							}
						}
						if(low > high) ans = max(ans, tmp);
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}