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;
typedef long long ll;

int mp[12][12];
int imos[10][12][12];

int get(int t, int x1, int y1, int x2, int y2){
    return imos[t][y2][x2] - imos[t][y2][x1 - 1] - imos[t][y1 - 1][x2] + imos[t][y1 - 1][x1 - 1];
}

int getMin(int x1, int y1, int x2, int y2){
    for(int i = 0; ;i++){
        if(get(i, x1, y1, x2, y2))return i;
    }
}
int getMax(int x1, int y1, int x2, int y2){
    for(int i = 9; ;i--){
        if(get(i, x1, y1, x2, y2))return i;
    }
}

int getMinEdge(int x1, int y1, int x2, int y2){
    int mn = getMin(x1, y1, x2, y1);
    mn = min(mn, getMin(x1, y2, x2, y2));
    mn = min(mn, getMin(x1, y1, x1, y2));
    mn = min(mn, getMin(x2, y1, x2, y2));
    return mn;
}

int main(){

    for(int d, w; cin >> d >> w, d; ){
        int ans = 0;
        for(int y = 1; y <= d; y++){
            for(int x = 1; x <= w; x++){
                cin >> mp[y][x];
            }
        }
        for(int i = 0; i < 10; i++){
            for(int y = 0; y < 12; y++){
                for(int x = 0; x < 12; x++){
                    imos[i][y][x] = 0;
                }
            }
            for(int y = 1; y <= d; y++){
                for(int x = 1; x <= w; x++){
                    imos[i][y][x] = imos[i][y][x - 1];
                    if(mp[y][x] == i)imos[i][y][x]++;
                }
            }
            for(int x = 1; x <= w; x++){
                for(int y = 1; y <= d; y++){
                    imos[i][y][x] += imos[i][y - 1][x];
                }
            }
        }
        for(int y1 = 1; y1 <= d; y1++){
            for(int x1 = 1; x1 <= w; x1++){
                for(int y2 = y1 + 2; y2 <= d; y2++){
                    for(int x2 = x1 + 2; x2 <= w; x2++){
                        int flg = 1;
                        int tmp = 0;
                        int edge = getMinEdge(x1, y1, x2, y2);
                        for(int i = 0; i < 10; i++){
                            int cnt = get(i, x1+1, y1+1, x2-1,y2-1);
                            if(edge <= i && cnt)flg = 0;      
                            tmp += (edge - i) * cnt;
                        }
                        if(flg){
                            ans = max(ans, tmp);
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;

ll d, w, e[11][11];

int main() {
  while (scanf("%lld%lld", &d, &w), d) {
    for (ll i = 0; i < d; i++) for (ll j = 0; j < w; j++) scanf("%lld", &e[i][j]);
    ll ans = 0;
    for (ll i = 0; i < d; i++) for (ll j = i+2; j < d; j++) {
      for (ll k = 0; k < w; k++) for (ll r = k+2; r < w; r++) {
        ll mn = 100000, mx = 0, temp = 0;
        for (ll x = i; x <= j; x++) for (ll y = k; y <= r; y++) {
          if (x == i || x == j || y == k || y == r) mn = min(mn, e[x][y]);
          else mx = max(e[x][y], mx), temp += e[x][y];
        }
        if (mn > mx) ans = max(ans, (j-i-1) * (r-k-1) * mn - temp);
        //if (mn > mx) printf("%lld, %lld, %lld, %lld(mn: %lld, mx: %lld)ok\n", i, j, k, r, mn, mx);
      }
    }
    printf("%lld\n", ans);
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
using namespace std;

int main (){
    int h,w;
	while(cin>>h>>w && h && w){
		vector<vector<int>> v(h,vector<int>(w,0));
		for(int i=0;i<h;i++)for(int j=0;j<w;j++) cin>>v[i][j];
		int ans=0;

		for(int y=0;y<10;y++){
			for(int x=0;x<10;x++){
				for(int height=2;height<10;height++){
					for(int width=2;width<10;width++){
						if(y+height>h-1 || x+width>w-1) continue;
						int min_height=1000000000;
						for(int wally=y;wally<=y+height;wally++){
							for(int wallx=x;wallx<=x+width;wallx++){
								if(wally==y || wally==y+height || wallx==x || wallx==x+width) min_height=min(min_height,v[wally][wallx]);
							}
						}

						bool height_ok=true;
						for(int yy=y+1;yy<y+height;yy++){
							for(int xx=x+1;xx<x+width;xx++){
								if(min_height <= v[yy][xx]) height_ok=false;
							}
						}
						if(!height_ok)continue;

						int water=0;
						for(int yy=y+1;yy<y+height;yy++){
							for(int xx=x+1;xx<x+width;xx++){
								water+=max(0,min_height - v[yy][xx]);
							}
						}
						ans=max(ans,water);
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	

}