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: while True:
    d, w = map(int, input().split())
    if d == 0:
        break
    area = []
    for _ in range(d):
        area.append(list(map(int, input().split())))
    a = 0
    for dp in range(1, d - 1):
        for wp in range(1, w - 1):
            for i in range(1, d - dp):
                for j in range(1, w - wp):
                    mawari = min(area[i - 1][j - 1:j + wp + 1] + area[i + dp][j - 1:j + wp + 1])
                    naka = 0
                    for ii in range(dp):
                        mawari = min(mawari, area[i + ii][j - 1], area[i + ii][j + wp])
                        naka = max([naka] + area[i + ii][j:j + wp])
                    if mawari > naka:
                        vn = 0
                        for ii in range(dp):
                            for jj in range(wp):
                                vn += mawari - area[i + ii][j + jj]
                        a = max(vn, a)
    print(a)

Python: def solve(d,w,f):
    ans = 0
#    print("aaa")
    for i in range(d-2):
        for j in range(i+2,d):
            for k in range(w-2):
                for l in range(k+2,w):
                    minOut = 10
                    for m in range(i,j+1):
                        if minOut > f[m][k]:
                            minOut = f[m][k]
                        if minOut > f[m][l]:
                            minOut = f[m][l]
                            
                    for m in range(k,l+1):
                        if minOut > f[i][m]:
                            minOut = f[i][m]
                        if minOut > f[j][m]:
                            minOut = f[j][m]
                    total = 0
                    for m in range(i+1,j):
                        for n in range(k+1,l):
                            total += minOut-f[m][n]
                            if minOut <= f[m][n]:
                                total -= 10**5
    #                print(total,i,j,k,l)
                    if ans < total:
                        ans = total
    return ans


while True:
    d,w = [int(i) for i in input().split()]
    if d==0:
        break
    f = []
    for i in range(d):
        f.append([int(i) for i in input().split()])
    print(solve(d,w,f))

Python: while True:
    d,w = map(int,input().split())

    if d == 0:
        break

    e = list(list(map(int,input().split()))for i in range(d))
    ans = 0
    for sx in range(w-2):
        for sy in range(d-2):
            for tx in range(sx+2,w):
                for ty in range(sy+2,d):
                    #外周の最大値
                    max_out = float('inf')
                    for i in range(sx,tx+1):
                        max_out = min(e[sy][i],e[ty][i],max_out)
                    for i in range(sy,ty+1):
                        max_out = min(e[i][sx],e[i][tx],max_out)
                    v = 0
                    for j in range(sx+1,tx):
                        for i in range(sy+1,ty):
                            v += max_out-e[i][j]
                            if max_out - e[i][j] <= 0:
                                v = -float('inf')
                    ans = max(v,ans)
    print(ans)

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

        garden = []
        for i in range(d):
            garden.append(list(map(int, input().split())))

        pondmax = 0
        for tly in range(len(garden)):
            for tlx in range(len(garden[0])):
                for bry in range(tly + 2, len(garden)):
                    for brx in range(tlx + 2, len(garden[0])):
                        # sampling
                        l_gray = []
                        d_gray = []
                        for spy in range(tly, bry + 1):
                            for spx in range(tlx, brx + 1):
                                if spy == tly or spy == bry or spx == tlx or spx == brx:
                                    d_gray.append(garden[spy][spx])
                                else: l_gray.append(garden[spy][spx])
                        if min(d_gray) > max(l_gray):
                            pond = 0
                            for depth in l_gray:
                                pond += min(d_gray) - depth
                            if pond > pondmax: pondmax = pond

        print(pondmax)
                        
main()