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<algorithm>
#include<vector>
 
using namespace std;
typedef long long ll;
int e[15][15];
int main() {
    while (1) {
        int d, w; 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];
            }
        }
        int Max = 0;
        for (int i = 1; i < d - 1; i++) {
            for (int j = 1; j < w - 1; j++) {
                for (int k = i; k < d - 1; k++) {
                    for (int l = j; l < w - 1; l++) {
                        int Min = 100;
                        for (int a = i - 1; a <= k + 1; a++) {
                            Min = min(Min, e[a][j - 1]);
                            Min = min(Min, e[a][l + 1]);
                        }
                        for (int b = j - 1; b <= l + 1; b++) {
                            Min = min(Min, e[i - 1][b]);
                            Min = min(Min, e[k + 1][b]);
                        }
                        int cnt = 0;
                        bool flag = true;
                        for (int c = i; c <= k; c++) {
                            for (int d = j; d <= l; d++) {
                                cnt += (Min - e[c][d]);
                                if (e[c][d] >= Min) {
                                    flag = false;
                                    break;
                                }
                            }
                        }
                        if (flag == true)Max = max(Max, cnt);
                    }
                }
            }
        }
        cout << Max << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


signed main(void) {
    int n,m;
    while(cin >> n >> m,n){
        vector<vector<int> > v(n,vector<int>(m));
        rep(i,n)rep(j,m)cin >> v[i][j];
        int ans = 0;
        rep(x1,n)rep(y1,m)rep(x2,n)rep(y2,m){
            if(!(x1+1 < x2 && y1+1 < y2))continue;
            int mi = 1e9;
            for(int i = y1; i <= y2; i++){
                mi = min(mi, v[x1][i]);
                mi = min(mi, v[x2][i]);
            }
            for(int i = x1; i <= x2; i++){
                mi = min(mi, v[i][y1]);
                mi = min(mi, v[i][y2]);
            }
            int ma = 0;
            int sum = 0;
            for(int i = x1+1; i < x2; i++)for(int j = y1+1; j < y2; j++){
                ma = max(ma, v[i][j]);
                sum += mi-v[i][j];
            }
            if(mi > ma) ans = max(ans, sum);
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>

int a[10][10];

int calc(int sx,int sy,int tx,int ty,int minh){
  int y = 0;
  for(int i = sx+1;i<tx;i++){
    for(int j = sy+1;j<ty;j++){
      if(minh > a[i][j]){
	return 0;
      }else{
	y += minh-a[i][j];
      }
    }
  }
      return y;
   
} 

int main (void)
{
  int d,w;

  while(1){
  int ans = 0;

  int a[10][10];

  std::cin>>d>>w;
  if(d == 0&&w == 0)break;

  for(int i = 0; i<d;i++){
    for(int k = 0;k<w;k++){
      std::cin>>a[i][k];
    }
  }

  for(int sx = 0;sx<d-2;sx++){
    for(int sy = 0;sy<w-2;sy++){
      for(int tx = sx+2;tx<d;tx++){
	for(int ty = sy+2;ty<w;ty++){
	  int minh = 10;
	  for(int i = sx;i<=tx;i++){
	    minh = std::min(minh,a[i][sy]);
	    minh = std::min(minh,a[i][ty]);
	  }
	  for(int i = sy;i<=ty;i++){
	    minh = std::min(minh,a[sx][i]);
	    minh = std::min(minh,a[tx][i]);
	  }

	  ans = std::max(ans,calc(sx,sy,tx,ty,minh));
	    }
      }
    }
  }

  std::cout << ans<<std::endl;
  }
  return 0;
}