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;
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1e9+7;
const LL LINF=1LL<<62;
const int INF=1<<17;

int main(){
    int d,w;
    while(cin >> d >> w){
        if(d==0&&w==0) break;
        int st[d][w];
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++){
                cin >> st[i][j];
            }
        }
        int ans=0;
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++){
                for(int y=3;y<=d;y++){
                    for(int x=3;x<=w;x++){
                        int mini=INF,maxi=-1;
                        if(d<i+y||w<j+x) continue;
                        int sum=0;
                        for(int s=0;s<y;s++){
                            for(int t=0;t<x;t++){
                                if(s==0||s==y-1||t==0||t==x-1){
                                    mini=min(mini,st[i+s][j+t]);
                                }
                                else{
                                    sum+=st[i+s][j+t];
                                    maxi=max(maxi,st[i+s][j+t]);
                                }
                            }
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h> 

using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF INT_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

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

    int ans=0;

    for(int x1=0;x1<w;x1++){
      for(int x2=x1+2;x2<w;x2++){
        for(int y1=0;y1<d;y1++){
          for(int y2=y1+2;y2<d;y2++){

            int exMin=INF;
            for(int x=x1;x<=x2;x++){
              exMin=min(exMin,e[y1][x]);
              exMin=min(exMin,e[y2][x]);
            }
            for(int y=y1;y<=y2;y++){
              exMin=min(exMin,e[y][x1]);
              exMin=min(exMin,e[y][x2]);
            }

            int inMax=0;
            for(int x=x1+1;x<x2;x++){
              for(int y=y1+1;y<y2;y++){
                inMax=max(inMax,e[y][x]);
              }
            }

            int sum=0;
            if(exMin>inMax){
              for(int x=x1+1;x<x2;x++){
                for(int y=y1+1;y<y2;y++){
                  sum+=(exMin-e[y][x]);
                }
              }
            }

            ans=max(ans,sum);
          }
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define test(a) printf("(%3d)", a)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int MAXN = 5e4 + 10;

int vis[11][11][11][11];
struct Node {
    int x, y, xx, yy;
    Node() {}
    Node(int x, int y, int xx, int yy):x(x),y(y),xx(xx),yy(yy) {}
} node;
queue<Node> q;

int n, m, a[11][11];
int ans;

int main (void) {
    while (scanf("%d%d", &n, &m) && (n||m)) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                scanf("%d", &a[i][j]);
            }
        }
        
        ans = 0;
        memset(vis,0,sizeof(vis));

        vis[2][2][n-1][m-1] = 1;
        q.push(Node(2,2,n-1,m-1));
        while (!q.empty()) {
	        node = q.front(); q.pop();
	        if (node.x > node.xx || node.y > node.yy) continue;
	        vis[node.x][node.y][node.xx][node.yy] = 1;

	        int low = 10, up = 0, tmp = 0;
	        for (int i = node.x-1; i <= node.xx+1; i++) {
	            low = min(low,a[i][node.yy+1]);
	            low = min(low,a[i][node.y-1]);
	        }
	   	    for (int j = node.y; j <= node.yy; j++) {
	            low = min(low,a[node.x-1][j]);
	            low = min(low,a[node.xx+1][j]);
	        }
	        for (int i = node.x; i <= node.xx; i++) {
	            for (int j = node.y; j <= node.yy; j++) {
	                up = max(a[i][j],up);
	                tmp += low - a[i][j];
	            }
	        }
	        if (up < low) ans = max(tmp,ans);
	        else {
	        	if (!vis[node.x+1][node.y][node.xx][node.yy])
	          		q.push(Node(node.x+1,node.y,node.xx,node.yy));
	        	if (!vis[node.x][node.y+1][node.xx][node.yy])
	           		q.push(Node(node.x,node.y+1,node.xx,node.yy));
	        	if (!vis[node.x][node.y][node.xx-1][node.yy])
	           		q.push(Node(node.x,node.y,node.xx-1,node.yy));
	        	if (!vis[node.x][node.y][node.xx][node.yy-1])
	           		q.push(Node(node.x,node.y,node.xx,node.yy-1));
	        }
        }
        printf("%d\n", ans);
    }
    return 0;
}