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>
#include<math.h>
#include<algorithm>
#include<utility>
#include<queue>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
int H,W;
int c[11][11];

int Calc(int h1,int w1,int h2,int w2){
    int minWall=10;
    for(int i=w1;i<=w2;i++){//上辺
        minWall=min(minWall,c[h1][i]);
    }
    for(int i=h1;i<=h2;i++){//右辺
        minWall=min(minWall,c[i][w2]);
    }
    for(int i=w1;i<=w2;i++){//下辺
        minWall=min(minWall,c[h2][i]);
    }
    for(int i=h1;i<=h2;i++){//左辺
        minWall=min(minWall,c[i][w1]);
    }
    int ans=0;
    for(int i=h1+1;i<h2;i++){
        for(int j=w1+1;j<w2;j++){
            if(c[i][j]>=minWall){
                return 0;
            }else{
                ans+=minWall-c[i][j];
            }
        }
    }
    return ans;
}
int main(){
    
    while(cin>>H>>W,H!=0){
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin>>c[i][j];
            }
        }
        int ans=0;
        for(int i=0;i<H-2;i++){
            for(int j=0;j<W-2;j++){
                for(int k=i+2;k<H;k++){
                    for(int l=j+2;l<W;l++){
                        ans=max(ans,Calc(i,j,k,l));
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include "bits//stdc++.h"
using namespace std;
typedef long long ll;
#define ALL(v) (v).begin(),(v).end()
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
int e[10][10];
int main() {
	int d, w;
	while (cin >> d >> w, d) {
		for (int i = 0; i < d; i++) {
			for (int j = 0; j < w; j++) {
				cin >> e[i][j];
			}
		}
		int ans = 0;
		for (int y1 = 0; y1 < d; y1++) {
			for (int x1 = 0; x1 < w; x1++) {
				for (int y2 = y1 + 2; y2 < d; y2++) {
					for (int x2 = x1 + 2; x2 < w; x2++) {
						int min_out = 1 << 30;
						int max_in = 0;
						for (int y = y1; y <= y2; y++) {
							for (int x = x1; x <= x2; x++) {
								if (y == y1 || y == y2 || x == x1 || x == x2) {
									min_out = min(min_out, e[y][x]);
								}
								else {
									max_in = max(max_in, e[y][x]);
								}
							}
						}
						if (min_out <= max_in) continue;
						int sum = 0;
						for (int y = y1; y <= y2; y++) {
							for (int x = x1; x <= x2; x++) {
								if (!(y == y1 || y == y2 || x == x1 || x == x2)) {
									sum += min_out - e[y][x];
								}
							}
						}
						ans = max(ans, sum);
					}
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>

class Solve {
private:
    using vi = std::vector<int>;
    using vvi = std::vector<vi>;
    using vb = std::vector<bool>;
    using vvb = std::vector<vb>;

    int D, W;
    vvi height;
    vvb visited;
    int calcMin(int t, int b, int l, int r)
    {
        int min{100};
        for (int i{t - 1}; i <= b + 1; i++)
            min = std::min({min, height[i][l - 1], height[i][r + 1]});
        for (int i{l - 1}; i <= r + 1; i++)
            min = std::min({min, height[t - 1][i], height[b + 1][i]});
        return min;
    }

    int calcCap(int t, int b, int l, int r, int h)
    {
        int ret{};
        for (int i{t}; i <= b; i++)
            for (int j{l}; j <= r; j++)
            {
                if (height[i][j] >= h) return -1;
                ret += h - height[i][j];
            }
        return ret;
    }
public:
    bool is_last_{};
    Solve()
    {
        scanf("%d%d", &D, &W);
        if (D == 0 && W == 0)
        {
            is_last_ = true;
            return;
        }
        height.resize(D + 2, vi(W + 2, -1));
        for (int i{1}; i <= D; i++)
            for (int j{1}; j <= W; j++)
                scanf("%d", &height[i][j]);
        int max{};
        for (int t{2}; t < D; t++)
            for (int b{t}; b < D; b++)
                for (int l{2}; l < W; l++)
                    for (int r{l}; r < W; r++)
                    {
                        int h{calcMin(t, b, l, r)};
                        max = std::max(max, calcCap(t, b, l, r, h));
                    }
        printf("%d\n", max);
    }
};

int main()
{
    while (!Solve().is_last_);

    return 0;
}