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>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	int h, w;
	while(cin >> h >> w, h){
		vector<vi> in(h, vi(w));
		rep(i, h) rep(j, w) cin >> in[i][j];
		
		auto check = [&](int y, int x, int Y, int X){
			
			int omin = inf, imax = -1, isum = 0;
			for(int i = y; i <= Y; i++) for(int j = x; j <= X; j++){
				bool inner = (i != y && i != Y) && (j != x && j != X);
				if(inner){
					isum += in[i][j];
					imax = max(imax, in[i][j]);
				}
				else omin = min(omin, in[i][j]);
			}
			//dbg(omin, imax, isum);
			if(imax < 0 || omin <= imax) return -1;
			return omin * (Y - y - 1) * (X - x - 1) - isum;
		};
		int ans = 0;
		rep(Y, h) rep(y, Y) rep(X, w) rep(x, X) ans = max(ans, check(y, x, Y, X));
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;


int d,w;
int height[10][10];

int _func(int s1, int s2, int t1, int t2){
    int _min = 11;
    int _max = 0;
    int sum = 0;
    for(int s=s1;s<=s2;s++){
        for(int t=t1;t<=t2;t++){
            if(s == s1 || s == s2 || t == t1 || t == t2){
                _min = min(_min, height[s][t]);
            }
            else{
                _max = max(_max, height[s][t]);
                sum += height[s][t];
            }
        }
    }
    if(_max < _min){
        return (s2-s1-1)*(t2-t1-1) * _min - sum;
    }
    return 0;
}
int main() {
    while (true) {
        cin >> d >> w;
        if (d == 0 || w == 0) break;

        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++){
                cin >> height[i][j];
            }
        }

        int m = 0;
        for(int s1=0;s1<d;s1++){
            for(int s2=s1+2;s2<d;s2++){
                for(int t1=0;t1<w;t1++){
                    for(int t2=t1+2;t2<w;t2++){
                        m = max(m, _func(s1,s2,t1,t2));
                    }
                }
            }
        }

        cout << m << endl;
    }
    return 0;
}

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

using namespace std;

vector< vector<int> > field;

int calc_agp(int row, int col, int rr, int cr) {
	int lim = (1 << 28);
	int sum,wmax;
	sum = wmax = 0;
	for (int i = 0; i < rr; ++i) {
		for (int j = 0; j < cr; ++j) {
			if (i == 0||j == 0||i == rr-1||j == cr-1) {
				lim = min(field[row+i][col+j],lim);
			}
			else {
				wmax = max(field[row+i][col+j],wmax);
				sum += field[row+i][col+j];
			}
		}
	}
	if (wmax >= lim) {
		return 0;
	}
	return ((lim*(rr-2)*(cr-2))-sum);
}

int main(void) {
	int d,w;
	string str;
	while (cin >> d >> w,d,w) {
		int res = 0;
		field = vector< vector<int> >(d,vector<int>(w));
		for (int i = 0; i < d; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> field[i][j];
			}
		}
		for (int i = 0; i < d-2; ++i) {
			for (int j = 0; j < w-2; ++j) {
				for (int dd = 3; dd+i < d+1; ++dd) {
					for (int dw = 3; dw+j < w+1; ++dw) {
						res = max(calc_agp(i,j,dd,dw),res);
					}
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}