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 main(int argc, char const *argv[])
{
	int d,w;
	while(cin>>d>>w,d+w)
	{
		int e[d][w],ans=0;
		for (int i = 0; i < d; ++i) for (int j = 0; j < w; ++j) cin>>e[i][j];
		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=10,inmax=-1;
						for (int m = i; m <= k; ++m) outmin=min(outmin,min(e[m][j],e[m][l]));
						for (int m = j; m <= l; ++m) outmin=min(outmin,min(e[i][m],e[k][m]));
						for (int m = i+1; m < k; ++m) for (int n = j+1; n < l; ++n) inmax=max(inmax,e[m][n]);
						if(outmin>inmax)
						{
							int cap=0;
							for (int m = i+1; m < k; ++m) for (int n = j+1; n < l; ++n) cap+=outmin-e[m][n];
							ans=max(ans,cap);
						}
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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)
	{
		memset(a,0,sizeof(a));
		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 main()
{
    for (int d, w;;) {
        cin >> d >> w;
        if (d == 0 and w == 0) { break; }
        vector<vector<int>> e(d, vector<int>(w));
        for (int i = 0; i < d; i++) {
            for (int j = 0; j < w; j++) { cin >> e[i][j]; }
        }
        int ans = 0;
        for (int i = 0; i < d; i++) {
            for (int j = i + 2; j < d; j++) {
                for (int k = 0; k < w; k++) {
                    for (int l = k + 2; l < w; l++) {
                        int h = 100;
                        for (int x = k; x <= l; x++) { h = min({h, e[i][x], e[j][x]}); }
                        for (int y = i; y <= j; y++) { h = min({h, e[y][k], e[y][l]}); }
                        int sum = 0;
                        bool ok = true;
                        for (int x = k + 1; x < l; x++) {
                            for (int y = i + 1; y < j; y++) {
                                if (e[y][x] >= h) { ok = false; }
                                sum += h - e[y][x];
                            }
                        }
                        if (ok) { ans = max(ans, sum); }
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main(){
  int d,w;
  int e[10][10]={};
  int ans=0;
  while(cin>>d>>w,d!=0||w!=0){
    for(int i=0; i<d; i++){
      for(int j=0; j<w; j++){
				cin>>e[j][i];
      }
    }
		ans=0;
    for(int x=0;x<=w-3;x++){
      for(int y=0;y<=d-3;y++){
				for(int sx=3;sx<=w-x;sx++){
					for(int sy=3;sy<=d-y;sy++){
						int inmax=0,outmin=10,sum=0;
						for(int s=x;s-x<sx;s++){
							for(int t=y;t-y<sy;t++){
								if(s==x||t==y||s-x==sx-1||t-y==sy-1){
									outmin=min(outmin,e[s][t]);
								}else{
									inmax=max(inmax,e[s][t]);
								}
							}
						}
						if(outmin>inmax){
							for(int s=x+1;s-x<sx-1;s++){
								for(int t=y+1;t-y<sy-1;t++){
									sum+=outmin-e[s][t];
								}
				      }
	    			  ans=max(ans,sum);
						}
					}
				}
      }
    }
    cout<<ans<<endl;
  }
}