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 <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

signed main(){
    while(true){
        int n, m;
        cin >> n >> m;

        if(n == 0 && m == 0){
            break;
        }

        vector<vector<int>> a(n, vector<int>(m));
        rep(i, n){
            rep(j, m){
                cin >> a[i][j];
            }
        }

        int ans = 0;
        for(int h = 3; h <= n; h++){
            for(int w = 3; w <= m; w++){
                rep(i, n-h+1){
                    rep(j, m-w+1){
                        int min = INF;
                        for(int k = i; k < i + h; k++){
                            for(int l = j; l < j + w; l++){
                                if(i < k && k < i+h-1 && j < l && l < j+w-1){
                                    continue;
                                }
                                if(min > a[k][l]){
                                    min = a[k][l];
                                }
                            }
                        }

                        int tmp = 0;
                        bool ok = true;
                        for(int k = i; k < i + h; k++){
                            for(int l = j; l < j + w; l++){
                                if(i == k || k == i+h-1 || j == l || l == j+w-1){
                                    continue;
                                }
                                if(min <= a[k][l]){
                                    ok = false;
                                }
                                tmp += min - a[k][l];
                            }
                        }

                        if(ok && ans < tmp){
                            ans = tmp;
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,b,e) for(int i=(b);i<(e);++i)
#define rep(i,e) for(int i=0;i<(e);++i)

int main() {
  int d, w;
  while(cin >> d >> w){
    if(d==0) return 0;
    int grid[d][w] = {};
    rep(i,d)rep(j,w) cin>>grid[i][j];
    int ans=0;

    rep(i, d-2)rep(j, w-2){
      for(int dd=2; i+dd<d; dd++){
        for(int ww=2; j+ww<w; ww++){
          int wmin = 1e9;
          int cur = 0;
          int good = true;
          REP(ii, i, i+dd+1){
            wmin = min(wmin, grid[ii][j]);
            wmin = min(wmin, grid[ii][j+ww]);
          }
          REP(jj, j, j+ww+1){
            wmin = min(wmin, grid[i][jj]);
            wmin = min(wmin, grid[i+dd][jj]);
          }
          REP(ii, i+1, i+dd)REP(jj, j+1, j+ww){
            if(grid[ii][jj]>=wmin){
              good = false;
              break;
            }
            else cur += wmin - grid[ii][jj];
          }
          if(good) ans = max(ans, cur);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
  for(;;) {
    int d, w;
    cin >> d >> w;
    if(d == 0 && w == 0) break;
    vector<vector<int>> es;
    for(int i = 0; i < d; i++) {
      vector<int> v;
      for(int j = 0; j < w; j++) {
        int c;
        cin >> c;
        v.push_back(c);
      }
      es.push_back(move(v));
    }
    auto f = [&es](int i, int j, int k, int l){
      int mn = es[i][j];
      for(int ii = i; ii <= k; ii++) {
        if(mn > es[ii][j]) mn = es[ii][j];
        if(mn > es[ii][l]) mn = es[ii][l];
      }
      for(int jj = j; jj <= l; jj++) {
        if(mn > es[i][jj]) mn = es[i][jj];
        if(mn > es[k][jj]) mn = es[k][jj];
      }
      int sum = 0;
      for(int ii = i + 1; ii < k; ii++) {
        for(int jj = j + 1; jj < l; jj++) {
          if(es[ii][jj] >= mn) return -1;
          sum += mn - es[ii][jj];
        }
      }
      return sum;
    };
    int mx = 0;
    for(int i = 0; i < d; i++) {
      for(int j = 0; j < w; j++) {
        for(int k = i + 2; k < d; k++) {
          for(int l = j + 2; l < w; l++) {
            int s = f(i, j, k, l);
            if(s < 0) continue;
            if(s > mx) mx = s;
          }
        }
      }
    }
    cout << mx << endl;
  }
  return 0;
}