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)
}

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(d,w):
        aa = [LI() for _ in range(d)]
        r = 0
        for i in range(d-2):
            for j in range(w-2):
                for k in range(i+2,d):
                    for l in range(j+2,w):
                        mi = inf
                        for m in range(i,k+1):
                            t = aa[m][j]
                            if mi > t:
                                mi = t
                            t = aa[m][l]
                            if mi > t:
                                mi = t
                        for m in range(j,l+1):
                            t = aa[i][m]
                            if mi > t:
                                mi = t
                            t = aa[k][m]
                            if mi > t:
                                mi = t
                        s = 0
                        for m in range(i+1,k):
                            for n in range(j+1,l):
                                if mi <= aa[m][n]:
                                    s = -inf
                                    break
                                s += mi - aa[m][n]
                        if r < s:
                            r = s
        return r


    while 1:
        n,m = LI()
        if n == 0:
            break
        rr.append(f(n,m))

    return '\n'.join(map(str, rr))


print(main())

Python: def check(ry, rx, x, y, f):
    tmpmin = float("inf")
    for yi in range(ry):
        tmpmin = min(tmpmin, f[yi + y][x])
    for xi in range(rx):
        tmpmin = min(tmpmin, f[y][xi + x])
    for yi in range(ry):
        tmpmin = min(tmpmin, f[yi + y][x + rx - 1])
    for xi in range(rx):
        tmpmin = min(tmpmin, f[ry - 1 + y][x + xi])
    res = 0

    for yi in range(y + 1, y + ry - 1):
        for xi in range(x + 1, x + rx - 1):
            res += tmpmin-f[yi][xi]
            if f[yi][xi] >= tmpmin:
                return 0
    return res

def main(d, w):
    field = [list(map(int, input().split())) for i in range(d)]
    res = 0
    for ry in range(3,d+1):
        for rx in range(3,w+1):
            for y in range(d - ry + 1):
                for x in range(w - rx + 1):
                    res = max(res, check(ry, rx, x, y, field))
    print(res)

while 1:
    d, w = map(int, input().split())
    if d == w == 0:
        break
    main(d, w)

Python: while True:
	d,w = map(int,input().split())
	if d+w == 0:break
	pond = [list(map(int,input().split())) for _ in range(d)]
	ans = 0
	for left in range(w-1):
		for right in range(w-1,left+1,-1):
			for top in range(d-1):
				for under in range(d-1,top+1,-1):
					outh = 10
					outh = min(outh,min(pond[top][left:right+1]))
					outh = min(outh,min(pond[under][left:right+1]))
					zpond = list(zip(*pond))
					outh = min(outh,min(zpond[left][top:under]))
					outh = min(outh,min(zpond[right][top:under]))
					pondh = 0
					for i in range(top+1, under):
						pondh = max(pondh,max(pond[i][left+1:right]))
					if pondh < outh:
						cap = outh*(under-top-1) * (right-left-1) - sum(sum(pond[i][left+1:right]) for i in range(top+1,under))
						ans = max(cap,ans)
	print(ans)

Python: from itertools import product
while True:
    d, w = map(int, input().split())
    if d == 0:
        exit()

    ans = 0
    e = [list(map(int, input().split())) for _ in range(d)]
    for si, sj in product(range(1, d - 1), range(1, w - 1)):
        for ti, tj in product(range(si + 1, d), range(sj + 1, w)):
            mx = 0
            for i, j in product(range(si, ti), range(sj, tj)):
                mx = max(mx, e[i][j])
            mn = 100
            for i, j in product([si - 1, ti], range(sj - 1, tj + 1)):
                mn = min(mn, e[i][j])
            for i, j in product(range(si - 1, ti + 1), [sj - 1, tj]):
                mn = min(mn, e[i][j])
            if mx >= mn:
                continue

            tmp = 0
            for i, j in product(range(si, ti), range(sj, tj)):
                tmp += mn - e[i][j]
            ans = max(ans, tmp)
    print(ans)