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 <stdio.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

int E[99][99];
int main(){
	int H,W;
	for(;~scanf("%d%d",&H,&W)&&H&&W;){
		int R=0;
		for(int h=0;h<H;h++)for(int w=0;w<W;w++){
			scanf("%d",E[h]+w);
		}
		for(int h0=0;h0<H-2;h0++)for(int h1=h0+2;h1<H;h1++)for(int w0=0;w0<W-2;w0++)for(int w1=w0+2;w1<W;w1++){
			int X=99,R0=0;
			for(int w=w0;w<=w1;w++)X=min(X,min(E[h0][w],E[h1][w]));
			for(int h=h0;h<=h1;h++)X=min(X,min(E[h][w0],E[h][w1]));
			for(int w=w0+1;w<w1;w++)for(int h=h0+1;h<h1;h++){
				if(E[h][w]>=X)R0=-1;
				if(R0>=0)R0+=X-E[h][w];
			}
			if(R0>0)R=max(R,R0);
		}
		printf("%d\n",R);
	}
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

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

	while(true) {
		int h,w;
		cin>>h>>w;
		if(h==0) break;
		int ma[h][w][h][w],fld[h][w];
		REP(i,h) REP(j,w) cin>>fld[i][j];
		// [l,r], [u,d]
		REP(l,w) {
			FOR(r,l,w) {
				REP(u,h) {
					FOR(d,u,h) {
						int MA=0;
						FOR(x,l,r+1) FOR(y,u,d+1) MA=max(MA,fld[y][x]);
						ma[u][r][d][l]=MA;
						//printf("u,r,d,l = %d %d %d %d ma = %d\n",u,r,d,l,ma[u][r][d][l]);
					}
				}
			}
		}
		int ans=0;
		FOR(r,2,w) {
			FOR(l,0,r-1) {
				FOR(d,2,h) {
					FOR(u,0,d-1) {
						//printf("in r,l,d,u = %d %d %d %d ",r,l,d,u);
						int MI=INF;
						FOR(x,l,r+1) MI=min(MI,min(fld[u][x],fld[d][x]));
						FOR(y,u,d+1) MI=min(MI,min(fld[y][l],fld[y][r]));
						//printf("mi = %d ma = %d\n",MI,ma[u+1][r-1][d-1][l+1]);
						if(MI<=ma[u+1][r-1][d-1][l+1]) continue;
						int sum=0;
						FOR(x,l+1,r) {
							FOR(y,u+1,d) {
								sum+=MI-fld[y][x];
							}
						}
						ans=max(ans,sum);
					}
				}
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}

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

int d, w;
vector<vector<int>> e;

int solve(int sx, int sy, int tx, int ty) {
    int rake = 10;
    int sum = 0;
    for (int i = sx; i <= tx; ++i) {
        for (int j = sy; j <= ty; ++j) {
            if (i == sx || i == tx || j == sy || j == ty) {
                rake = min(rake, e[i][j]);
            }
        }
    }
    for (int i = sx + 1; i < tx; ++i) {
        for (int j = sy + 1; j < ty; ++j) {
            if (e[i][j] >= rake) return 0;
            sum += rake - e[i][j];
        }
    }
    return sum;
}

int main(void) {
    while (cin >> d >> w, d) {
        e = vector<vector<int>>(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 sx = 0; sx < d; ++sx) {
            for (int sy = 0; sy < w; ++sy) {
                for (int tx = sx + 2; tx < d; ++tx) {
                    for (int ty = sy + 2; ty < w; ++ty) ans = max(ans, solve(sx, sy, tx, ty)); 
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using Int = int64_t;
const Int inf = 1LL << 53;

int main() {
   for(;;) {
      Int h, w;
      std::cin >> h >> w;
      if( h == 0 && w == 0 ) {
         break;
      }
      auto xss = std::vector<std::vector<Int>>(h);
      for(Int i = 0; i < h; ++i) {
         for(Int k = 0; k < w; ++k) {
            Int t;
            std::cin >> t;
            xss[i].push_back(t);
         }
      }
      Int res = 0;
      for(Int y1 = 0; y1 < h; ++y1) {
         for(Int x1 = 0; x1 < w; ++x1) {
            for(Int y2 = y1+2; y2 < h; ++y2) {
               for(Int x2 = x1+2; x2 < w; ++x2) {
                  bool valid = true;
                  Int round_min = inf;
                  for(Int y = y1; y <= y2; ++y) {
                     round_min = std::min(round_min, xss[y][x1]);
                     round_min = std::min(round_min, xss[y][x2]);
                  }
                  for(Int x = x1; x <= x2; ++x) {
                     round_min = std::min(round_min, xss[y1][x]);
                     round_min = std::min(round_min, xss[y2][x]);
                  }
                  Int t = 0;
                  for(Int y = y1+1; y <= y2-1; ++y) {
                     for(Int x = x1+1; x <= x2-1; ++x) {
                        if( xss[y][x] >= round_min ) {
                           valid = false;
                        }
                        t += round_min - xss[y][x];
                     }
                  }
                  if( valid ) {
                     res = std::max(res, t);
                  }
               }
            }
         }
      }
      std::cout << res << std::endl;
   }
}