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;

inline int ctoi(char c) { if(c < '0' || '9' < c) throw invalid_argument("ctoi error"); return c - '0'; }
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for(int i = (int)(k); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

template <typename T>
inline T gcd(T x, T y){
	if (x <= 0 || y <= 0) throw invalid_argument("gcd error: x <= 0 or y <= 0");
	
	if(x < y) swap(x, y);
	T r = x % y;

	while(r != 0){
		x = y;
		y = r;
		r = x % y;
	}

	return y;
}
template <typename T>
inline T lcm(T x, T y){
	if (x <= 0 || y <= 0) throw invalid_argument("lcm error: x <= 0 or y <= 0");

	return x * y / gcd(x, y);
}

vector<vector <int> > input;

int h_in_max(int lx, int ly, int rx, int ry){
	int m = input[ly + 1][lx + 1];

	REP(i, ly + 1, ry){
		REP(j, lx + 1, rx){
			if(m <= input[i][j]) m = input[i][j];
		}
	}

	return m;
}

int h_out_min(int lx, int ly, int rx, int ry){
	int m = input[ly][lx];

	REP(i, ly, ry + 1){
		REP(j, lx, rx + 1){
			if(i == ly || i == ry || j == lx || j == rx){
				if(input[i][j] <= m) m = input[i][j];
			}
		}
	}

	return m;
}

int point(int lx, int ly, int rx, int ry, int m){
	int ans = 0;

	REP(i, ly + 1, ry){
		REP(j, lx + 1, rx){
			ans += m - input[i][j];
		}
	}

	return ans;
}

int main(){
	vector<int> ans;

	while(true){
		int d, w;
		cin >> d >> w;

		if(d == 0 && w == 0) break;

		rep(i, d){
			vector<int> v;

			rep(j, w){
				int x;
				cin >> x;
				v.push_back(x);
			}

			input.push_back(v);
		}

		int tmpans = 0;

		// 左上
		REP(ly, 0, d - 2){
			REP(lx, 0, w - 2){
				// 右下
				REP(ry, ly + 2, d){
					REP(rx, lx + 2, w){
						int outmin = h_out_min(lx, ly, rx, ry);
						int inmax = h_in_max(lx, ly, rx, ry);

						if(inmax < outmin){
							int p = point(lx, ly, rx, ry, outmin);
							
							if(tmpans <= p){
								tmpans = p;
								// cout << p << endl;
							}
						}
					}
				}
			}
		}

		ans.push_back(tmpans);
		input.clear();
	}

	for(auto i : ans){
		cout << i << endl;
	}

	return 0;
}

C++: #include<iostream>
int main()
{
	int d,w;
	int e[100][100]={0};
	int min=10;
	int in=0;
	int x=0;
	int ans;
	
	while(d>0 && w>0){
		std::cin>>d>>w;
		if(d==0 && w==0) break;
		for(int i=0;i<d;i++){
			for(int j=0;j<w;j++){
				std::cin>>e[i][j];
			}
		}
		for(int i=0;i<d-2;i++){
			for(int j=i+2;j<d;j++){
				for(int k=0;k<w-2;k++){
					for(int l=k+2;l<w;l++){
						for(int m=i;m<=j;m++){
							for(int n=k;n<=1;n++){
								if(m==i || m==j || n==k || n==1){
									if(e[m][n]<min) min=e[m][n];
								}
							}
						}
						int max=0;
						for(int m=i+1;m<j;m++){
							for(int n=k+1;n<l;n++){
								if(e[m][n]>max) max=e[m][n];
								in+=e[m][n];
								x++;
							}
						}
						int tmp;
						if(min>max && (tmp(min*x-in))>ans){
							ans=tmp;
						}
					}
				}
			}
		}
		std::cout<<ans<<'\n';
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
#define repn(i,n) for(int i=1;i<=n;i++)
int a[15][15],n,m;
int main(){
    while(1){
        cin>>n>>m;
        if(n==0) return 0;
        repn(i,n) repn(j,m) cin>>a[i][j];
        int ans = 0;
        repn(i,n) for(int ii=i+2;ii<=n;ii++)
            repn(j,m) for(int jj=j+2;jj<=m;jj++){
                int mn = 1e9;
                for(int x=i;x<=ii;x++) mn = min(mn,a[x][j]);
                for(int x=i;x<=ii;x++) mn = min(mn,a[x][jj]);
                for(int y=j;y<=jj;y++) mn = min(mn,a[i][y]);
                for(int y=j;y<=jj;y++) mn = min(mn,a[ii][y]);
                int mx = 0, sum = 0;
                for(int x=i+1;x<ii;x++) for(int y=j+1;y<jj;y++){
                    mx = max(mx,a[x][y]);
                    sum += a[x][y];
                }
            
        if(mn > mx) ans = max(ans,mn*(ii-i-1)*(jj-j-1)-sum);
            }
        cout << ans << endl;
        
    }
}