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;
using ll = long long;
const int MAX = 1e9+7;

signed
main(){
		int d,w;
		while(cin >> d >> w && !(d==0 && w==0))
		{
				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;

				//縦A個, 横B個探索する
				for(int A=3;A<=d;A++)
				{
						for(int B=3;B<=w;B++)
						{
								//(y,x)が探索の初期値点
								for(int y=0;y<=d-A;y++)
								{
										for(int x=0;x<=w-B;x++)
										{
												//枠内の最大を求める
												int in_max = 0, in_sum = 0, in_num = 0;
												for(int a=y+1;a<=y+A-2;a++)
												{
														for(int b=x+1;b<=x+B-2;b++)
														{
																in_num++;
																in_sum += e[a][b];
																if(in_max < e[a][b])
																		in_max = e[a][b];
														}
												}
												//cout << in_sum << " " << in_max << endl;
													
												//枠外の最小を求める
												int out_min = MAX;
												for(int a=y;a<y+A;a++)
												{
														if(out_min > e[a][x]) out_min = e[a][x];
														if(out_min > e[a][x+B-1]) out_min = e[a][x+B-1];

												}
																									
												for(int b=x;b<x+B;b++)
												{
														if(out_min > e[y][b]) out_min = e[y][b];
														if(out_min > e[y+A-1][b]) out_min = e[y+A-1][b];

												}

												//cout << out_min << " " << in_max << endl;
												
												int sum = in_num * out_min - in_sum;
												if(out_min > in_max)
												{
														//cout << in_num << endl; 
														if(ans < sum) ans = sum;
												}
												
										}
								}

						}
				}
				cout << ans << endl;
		}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
const int MAX_N = 12;
const int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int h, w;
int table[MAX_N][MAX_N];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> h >> w, h | w)
    {
        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++) cin >> table[i][j];
        }
        int ans = 0;
        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++)
            {
                for(int k = i + 2; k < h; k++)
                {
                    for(int l = j + 2; l < w; l++)
                    {
                        int num = INF;
                        for(int p = i; p <= k; p++) num = min(num, table[p][j]);
                        for(int p = i; p <= k; p++) num = min(num, table[p][l]);
                        for(int q = j; q <= l; q++) num = min(num, table[i][q]);
                        for(int q = j; q <= l; q++) num = min(num, table[k][q]);
                        int cnt = 0;
                        bool flag = false;
                        for(int p = i + 1; p < k; p++)
                        {
                            for(int q = j + 1; q < l; q++)
                            {
                                if(table[p][q] >= num) flag = true;
                                cnt += (num - table[p][q]);
                            }
                        }
                        if(flag) cnt = -1;
                        ans = max(ans, cnt);
                    }
                }
            }
        }
        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)
	{
		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)
					break;
				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)
					break;
				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;
}