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 <bits/stdc++.h>
using namespace std;

int n, m;
int arr[20][20];

bool input() {
    scanf("%d %d", &n, &m);
    if (!n && !m) return 0;
    for (int i = 0 ; i < n ; i++ ) {
        for ( int j = 0 ; j < m ; j++ ) {
            scanf("%d", &arr[i][j]);
        }
    }
    return 1;
}

int check(int x, int y, int a, int b) {
    int out = 10000;
    for (int i = y ; i <= b ; i++ ) out = min(out, arr[x][i]);
    for (int i = y ; i <= b ; i++ ) out = min(out, arr[a][i]);
    for (int i = x+1 ; i < a ; i++ ) out = min(out, arr[i][y]);
    for (int i = x+1 ; i < a ; i++ ) out = min(out, arr[i][b]);

    int res = 0;
    for (int i = x+1 ; i < a ; i++ ) {
        for ( int j = y+1 ; j < b ; j++ ) {
            if ( arr[i][j] >= out  ) return 0;
            else res += out - arr[i][j];
        }
    }
    return res;
}

void solve() {
    int mx = 0;
    for (int i = 0 ; i < n ; i++ ) {
        for (int j = 0 ; j < m ; j++ ) {
            for (int a = i + 2 ; a < n ; a++ ) {
                for ( int b = j + 2 ; b < m ; b++ ) {
                    mx = max(mx, check(i, j, a, b));     
                }
            }
        }
    }
    cout <<mx << endl;
}

int main(){
    while(input())
    solve();
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int h,w;
    while(cin >>h >>w,h){
        vector<vector<int>> e(h,vector<int>(w));
        rep(i,h)rep(j,w) cin >>e[i][j];

        int ans = 0;
        rep(li,h)rep(lj,w)for(int ri=li+2; ri<h; ++ri)for(int rj=lj+2; rj<w; ++rj){

            int out = 9, in = 0;
            for(int y=li; y<=ri; ++y)for(int x=lj; x<=rj; ++x){
                if(y==li || y==ri || x==lj || x==rj){
                    out = min(out, e[y][x]);
                }
                else{
                    in = max(in, e[y][x]);
                }
            }

            if(out<=in) continue;
            int tmp = 0;
            for(int y=li+1; y<ri; ++y)for(int x=lj+1; x<rj; ++x) tmp += out - e[y][x];
            ans = max(ans, tmp);
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
int wall[10][10];
int calc(int sx,int sy,int ex,int ey){
  int m_wall=1000,cnt=0;
  for(int i=sx;i<=ex;i++){
    m_wall=min(m_wall,min(wall[i][sy],wall[i][ey]));
  }
  for(int i=sy;i<=ey;i++){
    m_wall=min(m_wall,min(wall[sx][i],wall[ex][i]));
  }
  for(int x=sx+1;x<ex;x++){
    for(int y=sy+1;y<ey;y++){
      if(wall[x][y]>=m_wall)return 0;
      cnt+=m_wall-wall[x][y];
    }
  }
  return cnt;
}
int main(){
  int d,w;
  cin >> d >>w;
  while(d!=0){
    int ans=0;
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
        cin >> wall[i][j];
      }
    }
    for(int sx=0;sx<d;sx++){
      for(int sy=0;sy<w;sy++){
        for(int ex=sx+1;ex<d;ex++){
          for(int ey=sy+1;ey<w;ey++){
            ans=max(ans,calc(sx,sy,ex,ey));
          }
        }
      }
    }
    cout <<ans<<endl;
    cin >> d >>w;
  }
}