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>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  for (int d, w; cin >> d >> w, d;) {
    vector<vector<int>> e(d, vector<int>(w));
    REP(i, d) REP(j, w) cin >> e[i][j];
    int ans = 0;
    REP(sx, d) REP(sy, w) {
      REP(tx, d) REP(ty, w) {
        if (sx >= tx || sy >= ty) continue;
        int mn = 1e9;
        REP(i, d) {
          if (i < sx || i > tx) continue;
          mn = min(mn, e[i][sy]);
          mn = min(mn, e[i][ty]);
        }
        REP(j, w) {
          if (j < sy || j > ty) continue;
          mn = min(mn, e[sx][j]);
          mn = min(mn, e[tx][j]);
        }
        bool f = false;
        int c = 0;
        REP(i, d) REP(j, w) {
          if (i <= sx || i >= tx) continue;
          if (j <= sy || j >= ty) continue;
          if (mn <= e[i][j]) f = true;
          c += mn - e[i][j];
        }
        if (f) continue;
        ans = max(ans, c);
      }
    }
    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=1<<30;
const int mod=1e9+7;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,w;;
    while(cin >> h >> w){
        if(h==0&&w==0){
            return 0;
        }
        vector<vector<int>> a(h,vector<int>(w));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> a[i][j];
            }
        }
        int ans=0;
        for(int i=1;i<h-1;i++){
            for(int j=1;j<w-1;j++){
                for(int k=i;k<h-1;k++){
                    for(int l=j;l<w-1;l++){
                        int t=10;
                        for(int x=i-1;x<=k+1;x++){
                            for(int y=j-1;y<=l+1;y++){
                                if(x>=i&&x<=k&&y>=j&&y<=l){
                                    continue;
                                }
                                chmin(t,a[x][y]);
                            }
                        }
                        bool f=true;
                        int sum=0;
                        for(int x=i;x<=k;x++){
                            for(int y=j;y<=l;y++){
                                if(a[x][y]>=t){
                                    f=false;
                                }
                                sum+=t-a[x][y];
                            }
                        }
                        if(f){
                            chmax(ans,sum);
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

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



int main(){
    while (true)
    {
        int d; int w;
        cin >> d >> w;
        if(d==0&&w==0){break;}
        vector<vector<int>> hyoukous(d,vector<int>(w));
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++){
                cin >> hyoukous[i][j];
            }
        }
        int ans = 0;

        for(int haba = 3;haba<w+1;haba++){
            for(int tks = 3;tks<d+1;tks++){
                for(int hdr=0;hdr<w-haba+1;hdr++){
                    
                    for(int ue = 0;ue<d-tks+1;ue++){
                        int mx = -1;
                        int mn = 10;
                        for(int a=hdr;a<hdr+haba;a++){
                            if(mn>hyoukous[ue][a])mn = hyoukous[ue][a];
                            if(mn>hyoukous[ue+tks-1][a])mn = hyoukous[ue+tks-1][a];
                        }
                        for(int b=ue;b<ue+tks;b++){
                            if(mn>hyoukous[b][hdr+haba-1])mn = hyoukous[b][hdr+haba-1];
                            if(mn>hyoukous[b][hdr])mn = hyoukous[b][hdr];
                        }

                        for(int c=hdr+1;c<hdr+haba-1;c++){
                            for(int dd=ue+1;dd<ue+tks-1;dd++){
                                if(mx<hyoukous[dd][c])mx = hyoukous[dd][c];
                            }
                        }
                        if(mx<mn){
                            int youryou = 0;
                            for(int c=hdr+1;c<hdr+haba-1;c++){
                            for(int dd=ue+1;dd<ue+tks-1;dd++){
                                youryou += mn - hyoukous[dd][c];
                            }
                        }
                        if(ans<youryou)ans = youryou;
                        }
                    }
                }
            }
        }

        cout << ans << endl;
    }
    
}