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 <cstdio>
#include <iostream>
using namespace std;
int N,M;
int a[12][12],ans;

int main () {
	while(1) {
			ans = 0;
		scanf("%d%d",&N,&M);
		if(N == 0) break;
		for(int i = 1;i <= N;++i) {
			for(int j = 1;j <= M;++j) {
				scanf("%d",&a[i][j]);
			}
		}

        for(int i1 = 1;i1 <= N;++i1) {
			for(int j1 = 1;j1 <= M;++j1) {
                for(int i2 = 1;i2 <= N;++i2) {
					for(int j2 = 1;j2 <= M;++j2) {


                        if(  (i1 - i2 > 0 ? i1 - i2 : i2 - i1) < 2 || (j1 - j2 > 0 ? j1 - j2 : j2 - j1) < 2)
							continue;

						int x1 = min(i1,i2);
						int y1 = min(j1,j2);
						int x2 = max(i1,i2);
						int y2 = max(j1,j2);


						int Min = 0x3f3f3f3f;

						for(int i = y1;i <= y2;++i) {
                            Min = min(Min,a[x1][i]);
                            Min = min(Min,a[x2][i]);
						}
						for(int i = x1;i <= x2;++i) {
                            Min = min(Min,a[i][y1]);
                            Min = min(Min,a[i][y2]);
						}

						int re = 0;bool f = 0;

						for(int i = x1 + 1;i <= x2 - 1;++i) {
							for(int j = y1 + 1;j <= y2 - 1;++j) {
                                re += Min - a[i][j];
                                if(a[i][j] >= Min) {
									f = 1;
									break;
                                }
							}
						}
						if(!f) {
							ans = max(ans,re);
						}

					}
                }
			}
        }
        printf("%d\n",ans);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i,x,n) for(int i=x;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=200005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int H,W;cin>>H>>W;
        if(H+W==0) break;
        vector<vector<int>> S(H,vector<int>(W));
        
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin>>S[i][j];
            }
        }
        
        int ans=0;
        
        for(int a=2;a<H;a++){
            for(int b=2;b<W;b++){
                for(int i=0;i+a<H;i++){
                    for(int j=0;j+b<W;j++){
                        int mi=INF,ma=0;
                        for(int x=i;x<=i+a;x++){
                            mi=min(mi,S[x][j]);
                            mi=min(mi,S[x][j+b]);
                        }
                        for(int y=j;y<=j+b;y++){
                            mi=min(mi,S[i][y]);
                            mi=min(mi,S[i+a][y]);
                        }
                        
                        for(int x=i+1;x<i+a;x++){
                            for(int y=j+1;y<j+b;y++){
                                ma=max(ma,S[x][y]);
                            }
                        }
                        
                        if(mi>ma){
                            int sum=0;
                            
                            for(int x=i+1;x<i+a;x++){
                                for(int y=j+1;y<j+b;y++){
                                    sum+=mi-S[x][y];
                                }
                            }
                            
                            ans=max(ans,sum);
                        }
                    }
                }
            }
        }
        
        cout<<ans<<endl;
    }
}

C++: #include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
template<typename T> void cmax(T &a, T b) { a = max(a, b); }
template<typename T> void cmin(T &a, T b) { a = min(a, b); }

signed main() {
  int d, w;
  while (cin >> d >> w) {
    if (d == 0 && w == 0) break;
    int mp[20][20] = {};
    rep(i, d) rep(j, w) cin >> mp[i][j];
    int ans = 0;
    rep(y, d - 2) {
      rep(x, w - 2) {
        loop(i, y + 2, d) {
          loop(j, x + 2, w) {
            int wall = INF;
            loop(k, x, j + 1) {
              cmin(wall, mp[y][k]);
              cmin(wall, mp[i][k]);
            }
            loop(k, y, i + 1) {
              cmin(wall, mp[k][x]);
              cmin(wall, mp[k][j]);
            }
            bool flag = true;
            int tmp = 0;
            loop(iny, y + 1, i) {
              loop(inx, x + 1, j) {
                if (wall <= mp[iny][inx]) flag = false;
                tmp += wall - mp[iny][inx];
              }
            }
            if (flag) cmax(ans, tmp);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}