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

int n,m;
int dt[11][11];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    while(scanf("%d %d",&n,&m)&&n*m!=0)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&dt[i][j]);
            }
        }

        int ans=0;
        for(int i=1;i+2<=n;i++)//行
        {
            for(int j=1;j+2<=m;j++)//列
            {
                for(int k=i+2;k<=n;k++)//行
                {
                    for(int t=j+2;t<=m;t++)//列
                    {
                        int mn=100;
                        for(int t1=j;t1<=t;t1++)
                            mn=min(mn,min(dt[i][t1],dt[k][t1]));
                        for(int t1=i;t1<=k;t1++)
                            mn=min(mn,min(dt[t1][j],dt[t1][t]));

                        bool flag=false;
                        int tmp=0;
                        for(int t1=i+1;t1<=k-1;t1++)
                        {
                            for(int t2=j+1;t2<=t-1;t2++)
                            {
                                if(dt[t1][t2]>=mn)
                                {
                                    tmp=0;
                                    flag=true;
                                    break;
                                }
                                else
                                    tmp+=mn-dt[t1][t2];
                            }
                            if(flag)
                                break;
                        }
                        ans=max(ans,tmp);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

using namespace std;

using lint = long long;
using ldouble = long double;

bool solve() {
    int H, W;
    cin >> H >> W;
    if (H == 0) return false;

    vector<vector<int>> pond(H, vector<int>(W));
    for (auto& v : pond) {
        for (auto& e : v) cin >> e;
    }

    auto cap = [&](int lx, int ly, int rx, int ry) {
        int H = 10;
        for (int x = lx; x <= rx; ++x) {
            H = min({H, pond[x][ly], pond[x][ry]});
        }
        for (int y = ly; y <= ry; ++y) {
            H = min({H, pond[lx][y], pond[rx][y]});
        }

        int sum = 0;
        for (int x = lx + 1; x <= rx - 1; ++x) {
            for (int y = ly + 1; y <= ry - 1; ++y) {
                if (pond[x][y] >= H) return 0;
                sum += H - pond[x][y];
            }
        }
        return sum;
    };

    int ans = 0;
    for (int lx = 0; lx < H; ++lx) {
        for (int ly = 0; ly < W; ++ly) {
            for (int rx = lx + 2; rx < H; ++rx) {
                for (int ry = ly + 2; ry < W; ++ry) {
                    ans = max(ans, cap(lx, ly, rx, ry));
                }
            }
        }
    }

    cout << ans << endl;
    return true;
}

int main() {
    while (solve()) {}
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=INT_MAX,MOD=1e9+7;

int main(){
    int d,w;
    while(cin>>d>>w&&d){
        int e[d][w]; rep(i,d)rep(j,w) cin>>e[i][j];
        int volume=0;

        for(int i=0;i<d-2;i++){
            for(int j=0;j<w-2;j++){
                for(int k=i+2;k<d;k++){
                    for(int l=j+2;l<w;l++){
                        int outmin=INF,inmax=0,edge=0;
                        for(int a=i;a<=k;a++){
                            for(int b=j;b<=l;b++){
                                if(a==i||a==k||b==j||b==l){
                                    outmin=min(outmin,e[a][b]);
                                }
                                else{
                                    inmax=max(inmax,e[a][b]);
                                    //cout<<inmax<<endl;
                                    edge+=e[a][b];
                                }
                            }
                        }
                        //cout<<outmin<<" "<<inmax<<" "<<edge<<endl;
                        if(outmin>inmax){
                            volume=max(volume,outmin*(k-i-1)*(l-j-1)-edge);
                        }
                    }
                }
            }
        }
        cout<<volume<<endl;
    }

    return 0;
}