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 <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long>vll;
typedef pair<int,int> pi;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

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

int out_min(int left,int right,int up,int down){
  int ret=INF;
  for(int i=left;i<=right;i++){
    ret=min(ret,e[up][i]);
    ret=min(ret,e[down][i]);
    // cout<<"debug: "<<ret<<endl;
  }
  for(int i=up;i<=down;i++){
    ret=min(ret,e[i][right]);
    ret=min(ret,e[i][left]);
    // cout<<"debug: "<<ret<<endl;
  }

  return ret;
}

int in_max(int left,int right,int up,int down){
  int ret=-INF;
  for(int i=up+1;i<=down-1;i++){
    for(int j=left+1;j<=right-1;j++){
      ret=max(ret,e[i][j]);
    }
  }

  return ret;
}

int calc_score(int left,int right,int up,int down,int depth){
  int ret=0;
  for(int i=up+1;i<=down-1;i++){
    for(int j=left+1;j<=right-1;j++){
      ret+=(depth-e[i][j]);
    }
  }
  
  return ret;
}

int main(){
  while(cin>>d>>w,d|w){
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
	cin>>e[i][j];
      }
    }
    int ans=0;

    /* i==up j==down*/
    for(int i=0;i<d-2;i++){
      for(int j=i+2;j<d;j++){
	/* k==right l==left*/
	for(int l=0;l<w-2;l++){
	  for(int k=l+2;k<w;k++){
	    int o_m=out_min(l,k,i,j);
	    int i_m=in_max(l,k,i,j);
	    if(i_m<o_m){
	      ans=max(ans,calc_score(l,k,i,j,o_m));
	    }
	  }
	}
      }
    }

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

C++: #include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[10][10];
main()
{
	while(cin>>n>>m,n)
	{
		for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>a[i][j];
		int ans=0;
		for(int x1=0;x1<n;x1++)for(int x2=x1+2;x2<n;x2++)
			for(int y1=0;y1<m;y1++)for(int y2=y1+2;y2<m;y2++)
			{
				int A=10,B=0;
				for(int i=x1;i<=x2;i++)for(int j=y1;j<=y2;j++)
				{
					if(i==x1||i==x2||j==y1||j==y2)A=min(A,a[i][j]);
					else B=max(B,a[i][j]);
				}
				if(A>B)
				{
					int now=0;
					for(int i=x1+1;i<x2;i++)for(int j=y1+1;j<y2;j++)
						now+=A-a[i][j];
					if(ans<now)ans=now;
				}
			}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
using namespace std;

int main() {
	const long long int mod = 1000000007;
	map<long long int, long long int>mp;
	long long int  s[200][200];
	//cout << fixed << setprecision(10);
	long long int d, w, a[200] = {}, res = 0, one = 0, two = 0, e[30][30] = {}, hasi_min = 99999, ike = 0;
	char ch[200001],onech,twoch;
	cin >> d>>w;

	while (1) {
		hasi_min = 99999999;
		res = 0;
		ike = 0;
		if (d == 0&&w==0) { return 0; }
		for (int i = 1; i <= d; i++) {
			for (int j = 1; j <= w; j++) {
				cin >> e[i][j];
			}
		}
		for (int i = 1; i <= d; i++) {
			for (int j = 1; j <= w; j++) {
				for (int p = 3; p+i-1 <= d ; p++) {
					for (int q = 3; q+j-1 <= w ; q++) {

						hasi_min = 99999999;
						for (int z = i; z <= i + p - 1; z++) {
							if (e[z][j] < hasi_min) { hasi_min = e[z][j]; }
						}
						for (int z = j; z <= j + q - 1; z++) {
							if (e[i][z] < hasi_min) { hasi_min = e[i][z]; }
						}
						for (int z = i; z <= i + p - 1; z++) {
							if (e[z][j+q-1] < hasi_min) { hasi_min = e[z][j + q - 1]; }
						}
						for (int z = j; z <= j + q - 1; z++) {
							if (e[i+p-1][z] < hasi_min) { hasi_min = e[i + p - 1][z]; }
						}
						for (int x = i + 1; x <= i + p - 2; x++) {
							for (int y = j + 1; y <= j + q - 2; y++) {
								if (e[x][y] >= hasi_min) { goto next; }
								else { res += hasi_min-e[x][y] ; }
							}
						}
						if (ike < res) { ike = res; }
					next:
						res = 0;
					}
				}
			}
		}
		cout << ike << endl;
		cin >> d>>w;
	}
}