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 <iostream>

using namespace std;

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

int main(void) {
    while (1) {
        cin >> d >> w;
        if (d == 0 && w == 0) {
            break;
        }
        for (int i = 0; i < d; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> e[i][j];
            }
        }

        long long maxcap = 0;
        for (int ymin = 0; ymin < d; ++ymin) {
            for (int ymax = 0; ymax < d; ++ymax) {
                for (int xmin = 0; xmin < w; ++xmin) {
                    for (int xmax = 0; xmax < w; ++xmax) {

                        if (ymin + 2 <= ymax && xmin + 2 <= xmax) {
//                        printf("%d %d %d %d\n", ymin, ymax, xmin, xmax);
                            int exminheight = 11;
                            for (int i = ymin; i <= ymax; ++i) {
                                for (int j = xmin; j <= xmax; ++j) {
                                    if (i == ymin || i == ymax || j == xmin || j == xmax) {
                                        exminheight = min(e[i][j], exminheight);
                                    }
                                }
                            }
//                        cout << exminheight << endl;

                            long long cap = 0;
                            bool flood = false;
                            for (int i = ymin + 1; i <= ymax - 1; ++i) {
                                for (int j = xmin + 1; j <= xmax - 1; ++j) {
                                    if (e[i][j] >= exminheight) {
                                        flood = true;
                                        break;
                                    }
                                    cap += exminheight - e[i][j];
                                }
                                if (flood) {
                                    break;
                                }
                            }
                            if (!flood) {
                                maxcap = max(maxcap, cap);
                            }
                        }

                    }
                }
            }
        }

        cout << maxcap << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define fs first
#define sc second
#define MAXN 500005
#define LIMIT 1500
#define LOGN 20
#define ll long long
#define pb push_back
#define ld long double
using namespace std;
int d,w;
int pond[15][15],pref[15][15];
int gar[15][15];
bool checkHori(int pos, int l, int r){
	if(l<0||r>w-1||pos<0||pos>d-1)return 0;
	for(int i=l;i<=r;i++)if(pond[pos][i]==1)return 0;
	return 1;
}
bool checkVerti(int pos, int l, int r){
	if(l<0||r>d-1||pos<0||pos>w-1)return 0;
	for(int i=l;i<=r;i++)if(pond[i][pos]==1)return 0;
	return 1;
}
int main(){
	ios::sync_with_stdio(0);
	while(cin>>d){
		if(d==0)return 0;
		cin>>w;
		rep(i,d){
			rep(j,w){
				cin>>gar[i][j];
				pond[i][j]=0;
			}
		}
		int best=0;
		rep(q,10){
			//cout<<q<<endl;
			rep(i,d){
				rep(j,w){
					if(gar[i][j]==q)pond[i][j]=1;
				}
			}
			rep(i,d){
				rep(j,w){
					pref[i][j]=pond[i][j];
					if(i!=0)pref[i][j]+=pref[i-1][j];
					if(j!=0)pref[i][j]+=pref[i][j-1];
					if(i!=0&&j!=0)pref[i][j]-=pref[i-1][j-1];
				}
			}
			//cout<<d<<' '<<w<<endl; 
			for(int r1=0;r1<d;r1++){
				for(int r2=r1;r2<d;r2++){
					for(int c1=0;c1<w;c1++){
						for(int c2=c1;c2<w;c2++){
							int area=pref[r2][c2];
							//cout<<r1<<' '<<r2<<' '<<c1<<' '<<c2<<endl;
							if(r1!=0)area-=pref[r1-1][c2];
							if(c1!=0)area-=pref[r2][c1-1];
							if(r1!=0&&c1!=0)area+=pref[r1-1][c1-1];
							if(area==(r2-r1+1)*(c2-c1+1)){
								//cout<<r1<<' '<<c1<<' '<<r2<<' '<<c2<<endl;
								if(checkHori(r1-1,c1-1,c2+1)&&checkHori(r2+1,c1-1,c2+1)&&
								checkVerti(c1-1,r1-1,r2+1)&&checkVerti(c2+1,r1-1,r2+1)){
									int cur=0;
									for(int i=r1;i<=r2;i++){
										for(int j=c1;j<=c2;j++){
											cur+=q+1-gar[i][j];
										}
									}
									best=max(best,cur);
								}
							}
						}
					}
				}
			}
		}
		cout<<best<<endl;
	}
	return 0;
}