Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

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)