Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

type Node struct {
	ID    int
	X, Y  int
	Color Color
	Nexts []*Node
}

func main() {
	for sc.Scan() {
		if sc.Text() == "0 0" {
			break
		}
		chunks := strings.Split(sc.Text(), " ")
		w, _ := strconv.Atoi(chunks[0])
		h, _ := strconv.Atoi(chunks[1])

		// Initialize map
		m := map[int]map[int]*Node{}
		for x := 1; x <= w; x++ {
			m[x] = map[int]*Node{}
			for y := 1; y <= h; y++ {
				m[x][y] = &Node{ID: x + y*w, X: x, Y: y}
			}
		}
		for x := 1; x <= w; x++ {
			for y := 1; y <= h; y++ {
				nexts := []*Node{}
				if n, ok := m[x-1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y-1]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x+1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y+1]; ok {
					nexts = append(nexts, n)
				}
				m[x][y].Nexts = nexts
			}
		}

		// Set colors
		queue := []*Node{}
		touchedIDs := map[int]struct{}{}
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					n := m[i+1][y]
					n.Color = ColorBlack
					if c == '@' {
						queue = append(queue, n)
						touchedIDs[n.ID] = struct{}{}
					}
				case '#':
					m[i+1][y].Color = ColorRed
				}
			}
		}

		// Search
		visitedIDs := map[int]struct{}{}
		for {
			if len(queue) == 0 {
				break
			}
			n := queue[0]
			queue = queue[1:]
			visitedIDs[n.ID] = struct{}{}
			for _, next := range n.Nexts {
				if next.Color == ColorRed {
					continue
				}
				if _, ok := touchedIDs[next.ID]; !ok {
					queue = append(queue, next)
					touchedIDs[n.ID] = struct{}{}
				}
			}
		}
		fmt.Println(len(visitedIDs))
	}
}

Python: import sys

sys.setrecursionlimit(10**6)

W = None
H = None
MMAP = None
D = [(0,1),(0,-1),(1,0),(-1,0)]

def main():
    global W,H,MMAP,D
    while True:
        W,H = map(int,input().split())
        if not W and not H:
            return
        MMAP = [[] for _ in range(H)] # tiles[y][x]
        sx,sy = 0,0
        for y in range(H):
            row = list(input())
            if "@" in row:
                sy = y
                sx = row.index("@")
            MMAP[y] = row
        # gprint(tiles)
        print(calc(sx,sy))

def calc(x,y):
    global W,H,MMAP,D

    if not (x >= 0 and x < W and y >= 0 and y < H):
        return 0
    
    if MMAP[y][x] == "v" or MMAP[y][x] == "#":
        return 0

    # gprint(MMAP)
    # input()

    ssum = 1
    MMAP[y][x] = "v"

    for dx,dy in D:
        nx = x+dx
        ny = y+dy
        ssum += calc(x+dx,y+dy)

    return ssum

def gprint(tiles):
    for r in tiles:
        print(str("|".join(r)))


if __name__ == '__main__':
    main()

Python: import sys
from collections import deque

def solve():
    while 1:
        w, h = map(int, sys.stdin.readline().split())

        if w == h == 0:
            return

        room = [[False]*w for i in range(h)]

        si = sj = -1

        for i in range(h):
            line = input()

            for j, ch in enumerate(line):
                if ch == '@':
                    room[i][j] = True
                    si = i
                    sj = j
                elif ch == '.':
                    room[i][j] = True

        ans = bfs(w, h, room, si, sj)

        print(ans)

dx = (1, 0, -1, 0,)
dy = (0, 1, 0, -1,)

def bfs(w, h, room, si, sj):
    visited = [[False]*w for i in range(h)]
    visited[si][sj] = True
    q = deque([(si, sj)])
    cnt = 1

    while q:
        ci, cj = q.popleft()

        for k in range(len(dx)):
            ni = ci + dy[k]
            nj = cj + dx[k]

            if 0 <= ni < h and 0 <= nj < w and room[ni][nj] and not visited[ni][nj]:
                visited[ni][nj] = True
                q.append((ni, nj))
                cnt += 1

    return cnt

if __name__ == '__main__':
    solve()

Python: #data????§???????[y???][x???]??§??????

if __name__ == "__main__":
    while 1:
        w,h = list(map(int,input().strip().split()))
        if w == h == 0:break
        data = []
        data = []
        for _ in range(h):data.append(list(input()))
        visited = []
        for i in range(h):
            if '@'in data[i]:stack= [(i,data[i].index('@'))]
        count = 0
        while len(stack) !=0:
            y,x= stack.pop()
            count +=1
            if y-1 >= 0 and not (y-1,x)in visited and data[y -1][x] == '.':
                visited.append((y-1,x))
                stack.append((y-1,x))
            if x -1 >= 0 and not (y,x-1)in visited and data[y][x-1] == '.':
                visited.append((y,x-1))
                stack.append((y,x-1))
            if y +1 < h and not (y+1,x)in visited and data[y +1][x] == '.':
                visited.append((y+1,x))
                stack.append((y+1,x))
            if x + 1 < w and not (y,x+1)in visited and data[y][x +1] == '.':
                visited.append((y,x+1))
                stack.append((y,x+1))

        print(count)

Python: while True:
    W, H = map(int, input().split())
    room = []
    for i in range(H):
        room.append(input())
    
    for i in range(H):
        for j in range(W):
            if room[i][j] == '@':
                (sx, sy) = (j, i)
            
    dx = (-1, 0, 1, 0)
    dy = (0, -1, 0, 1)
    visited = [[False] * W for i in range(H)]

    def rec(x, y):
        visited[y][x] = True
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < W and 0 <= ny < H and room[ny][nx] == '.':
                if not visited[ny][nx]:
                    rec(nx, ny)

    if W == 0 or H == 0:
        break
    rec(sx, sy)
    ans = 0
    for a in visited:
        for b in a:
            if b:
                ans += 1
    print(ans)

Python: def dfs(x, y, W, H):
    if x >= H or y >= W or x < 0 or y < 0:
        return
    if visited[x][y] == 1:
        return
    if gridgraph[x][y] == '.' or gridgraph[x][y] == '@':
        visited[x][y] = 1
        dfs(x+1, y, W, H)
        dfs(x, y+1, W, H)
        dfs(x-1, y, W, H)
        dfs(x, y-1, W, H)
    if gridgraph[x][y] == '#':
        return

    return


while True:
    W, H = map(int, input().split())
    if W == 0 and W == 0:
        break
    gridgraph = [list(input()) for _ in range(H)]

    visited = [[0]*W for _ in range(H)]

    sx = -1
    sy = -1
    for i, row in enumerate(gridgraph):
        for j in range(len(row)):
            if row[j] == '@':
                sx = i
                sy = j
    dfs(sx, sy, W, H)

    ans = 0
    for row in visited:
        ans += sum(row)
    print(ans)

Python: 
def get_start(tile, h, w):
	for i in range(w):
		for j in range(h):
			if tile[i][j] == '@':
				return (i, j)

dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]

while True:
	w, h = map(int, input().split())
	if (w,h) == (0,0):
		break
	tile = [list(input()) for i in range(h)]
	start = get_start(tile, w, h)
	count = 0
	q = [start]
	while q:
		(x,y) = q.pop()
		for d in dirs:
			xx = x+d[0]
			yy = y+d[1]
			if (0 <= xx and xx < h) and (0 <= yy and yy < w):
				if tile[xx][yy] == '.':
					tile[xx][yy] = '_'
					count += 1
					q.append((xx,yy))
	print(count+1)