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<stdio.h>

int main(){
	
	int a[11][11],d=1,w=1,i,t,D,W,y,x,Y,X,S,T,N,M;
	
	while(d!=0||w!=0){
		scanf("%d %d",&d,&w);
		if(d==0&&w==0) break;
		for(i=0;i<d;i++){
			for(t=0;t<w;t++){
				scanf("%d",&a[i][t]);
			}
		}
		//入力確認用ループ
		
		/*for(i=0;i<d;i++){
			printf("\n");
			for(t=0;t<w;t++){
				printf("%d ",a[i][t]);
			}
		}
		printf("\n");*/
		i=0;
		N=0;
		for(D=3;D<d+1;D++){//Dは長方形の高さ
			for(W=3;W<w+1;W++){//Wは長方形の幅
				for(y=0;y<d-D+1;y++){//yは長方形の左上のy座標
					for(x=0;x<w-W+1;x++){//xは長方形の左上のx座標
						//printf("%d %d %d %d\n\n",D,W,y,x);//現在の長方形の情報
						M=0;S=10;T=0;
						
						for(Y=y;Y<y+D;Y++){//Sは外枠の最小値
							if(Y==y||Y==y+D-1){
								for(X=x;X<x+W;X++){
									if(a[Y][X]<S){
										S=a[Y][X];
									}
								}
							}
							else {
								if(a[Y][x]<S){
									S=a[Y][x];
								}
								if(a[Y][x+W-1]<S){
									S=a[Y][x+W-1];
								}
							}
						}
						for(Y=y+1;Y<y+D-1;Y++){//Tは内側の最大値
							for(X=x+1;X<x+W-1;X++){
								if(a[Y][X]>T){
									T=a[Y][X];
								}
							}
						}
						//printf("%d %d\n\n",S,T);//今回の長方形のSとT
						if(S>T){                        //Mは今回の長方形の容量
							for(Y=y+1;Y<y+D-1;Y++){
								for(X=x+1;X<x+W-1;X++){
									M=M+S-a[Y][X];
								}
							}
						}
						if(N<M){//Nは今まで試した長方形の中の最大容量
							N=M;
						}
						i++;
					}
				}
			}
		}
		//printf("%d\n",i);//iは試行回数
		printf("%d\n",N);
	}
	return 0;
}

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

int h, w;
int memo[12][12] = {0};

int solve();

int main() {
  while(1) {
    cin >> h >> w;
    if(h + w == 0) break;
    for(int i = 0; i < h; ++i)
      for(int j = 0; j < w; ++j) cin >> memo[i][j];
    cout << solve() << endl;
  }
  return 0;
}

int solve() {
  int ans = 0;
  for(int u = 0; u < h; ++u)
    for(int d = u + 2; d < h; ++d)
      for(int l = 0; l < w; ++l)
        for(int r = l + 2; r < w; ++r) {
          int mn = 10000, sum = 0;
          bool ch = 1;
          for(int i = u; i <= d; ++i)
            mn = min(mn, min(memo[i][l], memo[i][r]));
          for(int i = l; i <= r; ++i)
            mn = min(mn, min(memo[u][i], memo[d][i]));
          for(int i = u + 1; i < d; ++i)
            for(int j = l + 1; j < r; ++j) {
              if(memo[i][j] >= mn) { ch = 0; }
              else
                sum += mn - memo[i][j];
            }
          if(ch) ans = max(ans, sum);
        }
  return ans;
}

C++: #include "bits/stdc++.h"
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)

signed main() {
	for (int d, w; cin >> d >> w && d;) {
		vector<vector<int>> e(d, vector<int>(w));
		rep(i, 0, d)rep(j, 0, w)cin >> e[i][j];
		int ans = 0;
		rep(i, 0, d)rep(j, 0, w)rep(ii, i + 2, d)rep(jj, j + 2, w) {
			int m = e[i][j];
			rep(ci, i, ii + 1) {
				m = min(m, e[ci][j]);
				m = min(m, e[ci][jj]);
			}
			rep(cj, j, jj + 1) {
				m = min(m, e[i][cj]);
				m = min(m, e[ii][cj]);
			}
			int k = e[i + 1][j + 1];
			int sum = 0;
			rep(ci, i + 1, ii)rep(cj, j + 1, jj) {
				k = max(k, e[ci][cj]);
				sum += m - e[ci][cj];
			}
			if (k >= m)continue;
			ans = max(sum, ans);
		}
		cout << ans << endl;
	}
	return 0;
}