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 <iostream>
#include <vector>

unsigned int d;
unsigned int w;
std::vector<std::vector<int> > input;

void mainCall();
int calcEdge(int x, int y, int w, int h);
int calcPond(int x, int y, int w, int h, int e);

int main(int argc, char const *argv[]) {


    std::cin >> d;
    std::cin >> w;

    while (d>2 && w>2) {
	int max_capacity = 0;
	    input.resize(w);
	    for (int i = 0; i < w; i++) {
	        input[i].resize(d);
	    }

	    for (int i = 0; i < d; i++) {
	        for (int j = 0; j < w; j++) {
	            std::cin >> input[j][i];
	        }
	    }

	    for (int i = 0; i < w-2; i++) {
	        for (int j = 0; j < d-2; j++) {
	            for (int k = 2; k < w-i; k++) {
	                for (int l = 2; l < d-j; l++) {
	                    int e = calcEdge(i,j,k,l);
	                    int c = calcPond(i,j,k,l,e);
	                    if (max_capacity < c) {
	                        max_capacity = c;
	                    }
	                }
	            }
	        }
	    }

	    std::cout << max_capacity <<std::endl;

	    std::cin >> d;
    	std::cin >> w;
	}
	return 0;
}


int calcEdge(int x, int y, int w, int h) {
    int min = input[x][y];
    for (int i = 0; i <= w; i++) {
        if (min > input[x+i][y]) {
            min = input[x+i][y];
        }
        if (min > input[x+i][y+h]) {
            min = input[x+i][y+h];
        }
    }
    for (int i = 0; i <= h; i++) {
        if (min > input[x][y+i]) {
            min = input[x][y+i];
        }
        if (min > input[x+w][y+i]) {
            min = input[x+w][y+i];
        }
    }
    return min;
}

int calcPond(int x, int y, int w, int h, int e) {
    int sum = 0;
    for (int i = 1; i <= w-1; i++) {
        for (int j = 1; j <= h-1; j++) {
            if (input[x+i][y+j] < e) {
                sum += e - input[x+i][y+j];
            } else {
                return -1;
            }
        }
    }
    return sum;
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>  
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000000000000000000000001
#define LONG_INF 10000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i,n) for(long long i = 0;i < n;++i)
int main() {
#define int long long 
	while (true) {
		int d, w;
		cin >> d >> w;
		if (d == 0) return 0;
		int cnter[20][20] = {};
		for (int i = 0; i < d; ++i) {
			for (int q = 0; q < w; ++q) {
				cin >> cnter[i][q];
			}
		}
		int ans = 0;
		REP(i, d) {
			REP(q, w) {
				for (int j = i+2; j < d; ++j) {
					for (int t = q+2; t < w; ++t) {
						int now_max = 100000;
						int now_minnest = 0;
						int geko = 0;
						int cnt = 0;
						for (int tea = i; tea <= j; ++tea) {
							for (int p = q; p <= t; ++p) {
								if (tea == i || tea == j || p == q || p == t) {
									now_max = min(now_max, cnter[tea][p]);
								}
								else {
									now_minnest = max(now_minnest, cnter[tea][p]);
									geko += cnter[tea][p];
									cnt++;
								}
							}
						}
						if (now_minnest < now_max) {
							ans = max(ans, now_max * cnt - geko);
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}