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;
long long aa;bool bb;char ch;
long long scan()
{
	while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');
	ch=='-'?(bb=1,aa=0):(bb=0,aa=ch-48);
	while(ch=getchar(),(ch>='0'&&ch<='9'))
		aa=aa*10+ch-48;
	return (bb?(-aa):(aa));
}
int i,j,k,x,y,z,p,q,n,m;
int lim;
int ans,tmp,flag;
int a[21][21];
int main()
{
	while(scanf("%d%d",&n,&m)&&n)
	{
		ans=0;
		for(i=1;i<=n;++i)
			for(j=1;j<=m;++j)
				a[i][j]=scan();
		for(lim=1;lim<=9;++lim)
		{
			for(i=1;i<n;++i)
			for(j=1;j<m;++j)//左上
			{
				if(a[i][j]<lim||a[i+1][j]<lim||a[i][j+1]<lim)
					continue;
				for(x=i+1;x<=n;++x)
				{
					if(a[x][j+1]>=lim)
						break;
					if(a[x+1][j]<lim)
						break;
				}
				if(a[x][j+1]<lim)
					continue;
				for(y=j+1;y<=m;++y)
				{
					if(a[i+1][y]>=lim)
						break;
					if(a[i][y+1]<lim)
						break;
				}
				if(a[i+1][y]<lim)
					continue;
				flag=0;
				for(z=i;z<=x;++z)
					if(a[z][y]<lim)
					{
						flag=1;
						break;
					}
				for(z=j;z<=y;++z)
					if(a[x][z]<lim)
					{
						flag=1;
						break;
					}
				if(flag==1)
					continue;
				tmp=0;
				for(p=i+1;p<x&&flag==0;++p)
					for(q=j+1;q<y;++q)
					{
						if(a[p][q]>=lim)
						{
							flag=1;
							break;
						}
						tmp+=lim-a[p][q];
					}
				if(flag==0)
				{
					if(tmp>ans)
						ans=tmp;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

// int minHori()

int solve(vector<vector<int>> field, int tate, int yoko) {
    int ret = 0;
    for (int i = 0; i <= field.size() - tate; i++) {
        for (int j = 0; j <= field[i].size() - yoko; j++) {
            // 堀の最小
            int minHori = 1000000007;
            for (int k = i; k < i + tate; k++) {
                minHori = min(field[k][j], minHori);
            }
            for (int k = i; k < i + tate; k++) {
                minHori = min(field[k][j + yoko - 1], minHori);
            }
            for (int l = j; l < j + yoko; l++) {
                minHori = min(field[i][l], minHori);
            }
            for (int l = j; l < j + yoko; l++) {
                minHori = min(field[i + tate - 1][l], minHori);
            }
            int poolWater = 0;
            // 内部の最大
            for (int k = i + 1; k < i + tate - 1; k++) {
                for (int l = j + 1; l < j + yoko - 1; l++) {
                    if (field[k][l] >= minHori) goto fackin;
                    // cout << "minHori" << minHori << "tate" << tate << "yoko" << yoko << "field"
                    //      << field[k][l] << endl;
                    poolWater += minHori - field[k][l];
                    // cout << poolWater << endl;
                }
            }
            ret = max(poolWater, ret);
        fackin:;
        }
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int D, W;
    while (1) {
        cin >> D >> W;
        if (!D & !W) break;
        vector<vector<int>> field(D, vector<int>(W));
        for (int i = 0; i < D; i++) {
            for (int j = 0; j < W; j++) {
                cin >> field[i][j];
            }
        }
        int ans = 0;
        for (int i = 3; i <= D; i++) {
            for (int j = 3; j <= W; j++) {
                ans = max(solve(field, i, j), ans);
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
#define vec(T) vector<T>
#define mat(T) vector<vector<T>>
using namespace std;

const int INF = 1 << 28;

int main() {
    int d, w;
    while(cin >> d >> w, d || w) {
	mat(int) n(d, vec(int)(w));

	for (auto& i : n) for (auto& j : i) cin >> j;

	int ans = 0;
	for (int sx = 0; sx < d; sx++) {
	    for (int sy = 0; sy < w; sy++) {
		for (int gx = sx + 2; gx < d; gx++) {
		    for (int gy = sy + 2; gy < w; gy++) {
			int min_value = INF;

			for (int i = sx; i <= gx; i++) {
			    min_value = min(min_value, n[i][sy]);
			    min_value = min(min_value, n[i][gy]);
			}

			for (int i = sy; i <= gy; i++) {
			    min_value = min(min_value, n[sx][i]);
			    min_value = min(min_value, n[gx][i]);
			}

			int sum = 0;
			bool flag = true;
			for (int i = sx + 1; flag && i < gx; i++) {
			    for (int j = sy + 1; flag && j < gy; j++) {
				if (n[i][j] < min_value) {
				    sum += min_value - n[i][j];
				} else {
				    flag = false;
				}
			    }
			}

			if (flag) {
			    ans = max(ans, sum);
			}
		    }
		}
	    }
	}

	cout << ans << endl;
    }

    return 0;
}