import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    ptr = 0
    n = int(input[ptr])
    ptr += 1
    m = int(input[ptr])
    ptr += 1
    
    grid = []
    for _ in range(n):
        grid.append(input[ptr])
        ptr += 1
    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]
    vis = [[0]*m for _ in range(n)]
    q = deque()
    col = 1
    
    for i in range(n):
        for j in range(m):
            if grid[i][j] == '0' and vis[i][j] == 0:
                vis[i][j] = col
                q.append((i, j))
                while q:
                    x, y = q.popleft()
                    for d in range(4):
                        nx = x + dx[d]
                        ny = y + dy[d]
                        if 0<=nx<n and 0<=ny<m and grid[nx][ny] == '0' and vis[nx][ny] == 0:
                            vis[nx][ny] = col
                            q.append((nx, ny))
                col += 1
    dis = [[-1]*m for _ in range(n)]
    q = deque()
    
    for i in range(n):
        for j in range(m):
            if vis[i][j] != 0:
                dis[i][j] = 0
                q.append((i, j))
    
    res = 0
    while q:
        x, y = q.popleft()
        for d in range(4):
            nx = x + dx[d]
            ny = y + dy[d]
            if 0<=nx<n and 0<=ny<m:
                if dis[nx][ny] == -1:
                    dis[nx][ny] = dis[x][y] + 1
                    if vis[nx][ny] != 0 and vis[nx][ny] != vis[x][y]:
                        res = dis[x][y]
                        print(n * m - res)
                        return
                    q.append((nx, ny))

if __name__ == "__main__":
    main()