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 f(vector<vector<int> > &e, int u, int d, int l, int r){

    int res = 0;
    int hight = 10;
    for(int i = u; i <= d; i++){
        hight = min(hight, e[i][l]);
        hight = min(hight, e[i][r]);
    }

    for(int j = l; j <= r; j++){
        hight = min(hight, e[u][j]);
        hight = min(hight, e[d][j]);
    }

    for(int i = u + 1; i < d; i++){
        for(int j = l + 1; j < r; j++){
            if(hight <= e[i][j]) return 0;
            res += hight - e[i][j];
        }
    }

    return res;
}

int main(){

    while(1){
        
        int h, w; cin >> h >> w;
        if(!h) break;
        vector<vector<int> > e(h, vector<int> (w));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> e[i][j];
            }
        }

        int ans = 0;
        
        //[u, d]
        //[l, r]

        for(int u = 0; u < h; u++){
            for(int l = 0; l < w; l++){
                for(int d = u + 2; d < h; d++){
                    for(int r = l + 2; r < w; r++){
                        ans = max(ans, f(e, u, d, l, r));
                    }
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

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


int solve(int H, int W) {
    vector<vector<int>> field(H, vector<int>(W));
    for (int h = 0; h < H; h++) {
        for (int w = 0; w < W; w++) {
            cin >> field[h][w];
        }
    }
    

    int ret = 0;
    
    for (int sh = 0; sh < H; sh++) {
        for (int sw = 0; sw < W; sw++) {
            for (int eh = sh+2; eh < H; eh++) {
                for (int ew = sw+2; ew < W; ew++) {
                    int outer = 100;
                    int inner = -1;
                    int sum = 0;
                    for (int h = 0; h < H; h++) {
                        for (int w = 0; w < W; w++) {
                            if (sh <= h && h <= eh && sw <= w && w <= ew) {
                                if (h == sh || h == eh || w == sw || w == ew) {
                                    outer = min(outer, field[h][w]);
                                } else {
                                    sum -= field[h][w];
                                    inner = max(inner, field[h][w]);
                                }
                            }
                            if (h == H-1 && w == W-1 && outer > inner) {
                                sum += outer * (eh - sh - 1) * (ew - sw - 1);
                                ret = max(sum, ret);
                            } 
                        }
                    }
                }
            }
        }
    }
    return ret;
}

int main(void){
    int h, w;
    vector<int> ans;
    while (1) {
        cin >> h >> w;
        if (!h && !w) break;
        ans.push_back(solve(h, w));
    }
    for (auto &i : ans) {
        cout << i << endl;
    }
    
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
const ll MOD = 1e9+7;

int main(){
  int d,w;
  while(cin>>d>>w,d){
	int e[11][11];
	int 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 x=i+2;x<d;x++)
		  for(int y=j+2;y<w;y++){
			int wl=11;
			int ik=0;
			for(int k=i;k<=x;k++){
			  wl=min(wl,e[k][j]);
			  wl=min(wl,e[k][y]);
			}

			for(int k=j;k<=y;k++){
			  wl=min(wl,e[i][k]);
			  wl=min(wl,e[x][k]);
			}

			int sum=0;
			for(int k=i+1;k<x;k++)
			  for(int l=j+1;l<y;l++){
				ik=max(ik,e[k][l]);
				sum+=e[k][l];
			  }
		  
			if(wl<=ik)continue;
			ans=max(ans,wl*(x-i-1)*(y-j-1)-sum);
		  }
	cout<<ans<<endl;
  }
  return 0;
}