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: #!/usr/bin/env python
# conding: utf

from logging import getLogger, StreamHandler, NullHandler, DEBUG, INFO
logger = getLogger(__name__)
#handler = StreamHandler()
handler = NullHandler()
logger.setLevel(DEBUG)
handler.setLevel(DEBUG)
logger.addHandler(handler)
logger.propagate = False

g_field = []
g_count = 0
g_W = -1
g_H = -1

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

def dfs(x, y):
    global g_field
    global g_count
    global g_W
    global g_H


    g_count += 1

    g_field[y][x] = "-"

    for dx, dy in OFFSETS:
        logger.debug("diff: {} {}".format(dx, dy))
        new_x = x + dx
        new_y = y + dy

        if new_x < 0 or new_x >= g_W:
            continue
        if new_y < 0 or new_y >= g_H:
            continue
        new_tile = g_field[new_y][new_x]
        if new_tile == "#" or new_tile == "-":
            continue

        logger.debug("{}, {}".format(new_x, new_y))
        dfs(new_x, new_y)


if __name__ == "__main__":
    while True:
        g_W, g_H = [int(s) for s in input().strip().split()]

        if g_W == 0 and g_H == 0:
            break
        
        g_field = []
        for _ in range(g_H):
            row = list(input().strip())
            g_field.append(row)
        g_count = 0

        for y in range(g_H):
            for x in range(g_W):
                if g_field[y][x] == "@":
                    start_x = x
                    start_y = y
                    break

        dfs(start_x, start_y)

        print(g_count)

Python: from collections import deque
while True:
    W,H = map(int,input().split())
    if W == 0: break
    src = [input() for i in range(H)]
    for y,row in enumerate(src):
        x = row.find('@')
        if x < 0: continue
        sx,sy = x,y
        break
    visited = [[0 for x in range(W)] for y in range(H)]
    visited[sy][sx] = 1
    q = deque([(sx,sy)])
    ans = 1
    while q:
        x,y = q.popleft()
        for dx,dy in zip([1,0,-1,0],[0,1,0,-1]):
            nx,ny = x+dx,y+dy
            if not(0 <= nx < W) or not(0 <= ny < H): continue
            if src[ny][nx] == '#' or visited[ny][nx]: continue
            visited[ny][nx] = 1
            ans += 1
            q.append((nx,ny))
    print(ans)

Python: import collections
while True:
    w,h = map(int,input().split())
    if w==0 and h==0:
        break
    tile=[]
    tile.append(["#"]*(w+2))
    for _ in range(h):
        temp= list(input())
        temp=  ["#"]+ temp + ["#"]
        tile.append(temp)
    tile.append(["#"]*(w+2))

    route=[[[0,0,0,0] for i in range(w)] for j in range(h)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            if tile[i+1][j] ==".":
                route[i-1][j-1][0]= 1
            if tile[i-1][j] ==".":
                route[i-1][j-1][1]= 1
            if tile[i][j+1] ==".":
                route[i-1][j-1][2]= 1
            if tile[i][j-1] ==".":
                route[i-1][j-1][3]= 1
            if tile[i][j]=="@":
                st=[i-1,j-1]

    dir= [[1,0],[-1,0],[0,1],[0,-1]]
    d=[[0]*w for i in range(h)]
    d[st[0]][st[1]]=1
    q= collections.deque()
    q.append(st)
    while len(q)> 0:
        cur = q.popleft()
        for i in range(4):
            if route[cur[0]][cur[1]][i]==1 and d[cur[0]+dir[i][0]][cur[1]+dir[i][1]]==0:
                d[cur[0]+dir[i][0]][cur[1]+dir[i][1]]=1
                q.append([cur[0]+dir[i][0],cur[1]+dir[i][1]])

    ans=0
    for i in range(h):
        for j in range(w):
            if d[i][j] == 1:
                ans+=1
    print(ans)

Python: from queue import Queue

def calcReachableTileNumByBFSearch(room, start_pos):
    q = Queue()
    q.put(start_pos)

    count = 1
    while not q.empty():
        cur_pos = q.get()
        D0 = [1, 0, -1, 0]
        D1 = [0, -1, 0, 1]
        for d in range(4):
            nc = [0,0]
            nc[0] = cur_pos[0]+D0[d]
            nc[1] = cur_pos[1]+D1[d]
            if room[nc[0]][nc[1]] == '.':
                room[nc[0]][nc[1]] = '*'
                q.put([nc[0],nc[1]])
                count +=1
    return count


def solve(width, height):
    room = [['#' for i in range(width+2)] for j in range(height+2)]
    for i in range(height):
        row = input()
        for j in range(width):
            room[i+1][j+1] = row[j]
            if room[i+1][j+1] == '@':
                start = [i+1,j+1]

    return calcReachableTileNumByBFSearch(room, start)


def main():
    while True:
        width, height = map(int, input().split())
        if width == 0 and height == 0:
            break
        print(solve(width, height))


if __name__ == "__main__":
    main()

Python: from collections import deque

while True:
    W, H = map(int, input().split())
    if W | H == 0:
        break
    tiles = [input() for i in range(H)]
    first = ()
    for h in range(H):
        index = tiles[h].find("@")
        if index != -1:
            first = (index, h)
            break
    memo = {first: 0}
    que = deque([first])
    count = 1
    while len(que) > 0:
        x, y = que.popleft()
        for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if (x + dx, y + dy) not in memo:
                if (0 <= x + dx < W) and (0 <= y + dy < H) and tiles[y + dy][x + dx] == ".":
                    count += 1
                    memo[(x + dx, y + dy)] = 1
                    que.append((x + dx, y + dy))
    print(count)

Python: import Queue

while True:
    w, h = [int(_) for _ in raw_input().split()]
    if w == h == 0:
        break
    dat = [list(raw_input()) for _ in xrange(h)]
    queue = Queue.Queue()
    for y, r in enumerate(dat):
        if "@" in r:
            queue.put([r.index("@"), y])

    while not queue.empty():
        x, y = queue.get()
        for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
            if not (0 <= x + dx < w) or not (0 <= y + dy < h):
                continue
            if dat[y + dy][x + dx] == ".":
                dat[y + dy][x + dx] = "@"
                queue.put([x + dx, y + dy])

    ans = 0
    for r in dat:
        ans += r.count("@")

    print(ans)