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: while True:
    W,H = [int(x) for x in input().split()]
    if W==0 and H==0:
        exit()
    tiles = []
    X = 0
    Y = 0
    for i in range(H):
        tiles.append(input())
        if '@' in tiles[i]:
            Y = i
            X = tiles[i].index('@')
    d = [[9999 for j in range(H*W)] for i in range(H*W)]
    for i in range(H*W):
        d[i][i] = 0
    for i in range(H):
        for j in range(W):
            if j>0:
                if tiles[i][j] in '.@' and tiles[i][j-1] in '.@':
                    d[W*i+j][W*i+j-1] = 1
            if j<W-1:
                if tiles[i][j] in '.@' and tiles[i][j+1] in '.@':
                    d[W*i+j][W*i+j+1] = 1
            if i>0:
                if tiles[i][j] in '.@' and tiles[i-1][j] in '.@':
                    d[W*i+j][W*i+j-W] = 1
            if j<H-1:
                if tiles[i][j] in '.@' and tiles[i+1][j] in '.@':
                    d[W*i+j][W*i+j+W] = 1
    for k in range(H*W):
        for i in range(H*W):
            for j in range(H*W):
                if d[i][j] > d[i][k]+d[k][j]:
                    d[i][j] = d[i][k]+d[k][j]
    cnt = 0
    for i in range(H*W):
        if d[X+Y*H][i]<9999:
            cnt += 1
    print(cnt)

Python: def location(field, wanted):
    for i, row in enumerate(field):
        for j, cell in enumerate(row):
            if cell == wanted:
                return i, j
def count(field, wanted):
    result = 0
    for row in field:
        for cell in row:
            if cell == wanted:
                result += 1
    return result
def neighborhood(center, w, h):
    i, j = center
    return filter(lambda x: 0 <= x[0] and x[0] < h and 0 <= x[1] and x[1] < w,
                  [                (i - 1, j    ),
                   (i    , j - 1),                 (i    , j + 1),
                                   (i + 1, j    )])
printed_later = []
while True:
    #入力
    w, h = map(int, input().split())
    if(w == 0 and h == 0):
        break
    field = []  # strの配列すなわちcharの配列の配列 '.':黒 '#':赤 '@':スタート (',':到達可能)
    for i in range(h):
        field.append(list(input()))
    #出力　幅優先探索
    queue = [location(field,'@')]
    field[queue[0][0]][queue[0][1]] = ','
    while len(queue) > 0:
        for cell in neighborhood(queue.pop(0), w, h):
            if field[cell[0]][cell[1]] == '.':
                queue.append(cell)
                field[cell[0]][cell[1]] = ','
    printed_later.append(count(field, ','))
for line in printed_later:
    print(line)

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

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

    while True:
        w,h = LI()
        if w == 0:
            break
        a = [S() for _ in range(h)]
        q = []
        f = collections.defaultdict(bool)
        for i in range(h):
            for j in range(w):
                if a[i][j] == '@':
                    q.append((i,j))
        qi = 0
        while len(q) > qi:
            i,j = q[qi]
            qi += 1
            for di,dj in dd:
                ni = i + di
                nj = j + dj
                if 0 <= ni < h and 0 <= nj < w and a[ni][nj] == '.' and not f[(ni,nj)]:
                    f[(ni,nj)] = True
                    q.append((ni,nj))
        rr.append(len(q))

    return '\n'.join(map(str, rr))


print(main())

Python: # -*- coding: utf-8 -*-
'''
所要時間は、分位であった。
'''


# ライブラリのインポート
#import re #正規表現
import sys
input = sys.stdin.readline
#import heapq
#import bisect
#import collections
#import math
#from pprint import pprint

def main():
    while 1:
        w, h = map(int, input().split())
        if w == 0 and h == 0: break
        mp = []
        for i in range(h):
            mp.append(list(input().strip()))
            if "@" in mp[-1]:
                stj = mp[-1].index("@")
                sti = i
        flag = [[0]*w for i in range(h)]
        #pprint(flag)
        flag = dfs(sti, stj, flag, mp)
        ans = sum([flag[i].count(1) for i in range(h)])
        print(ans)
        #pprint(flag)
def dfs(rw, cm, flag, mp):
    flag[rw][cm] = 1
    #print(rw,cm)
    #pprint(flag)
    try:
        c = mp[rw][cm-1]
        b = flag[rw][cm-1]
    except: c = "#"
    if c == "." and b == 0 and rw>=0 and cm>0: dfs(rw, cm-1, flag, mp)
    try:
        c = mp[rw-1][cm]
        b = flag[rw-1][cm]
    except: c = "#"
    if c == "." and b == 0 and rw>0 and cm>=0: dfs(rw-1, cm, flag, mp)
    try: 
        c = mp[rw][cm+1]
        b = flag[rw][cm+1]
    except: c = "#"
    if c == "." and b == 0: dfs(rw, cm+1, flag, mp)
    try: 
        c = mp[rw+1][cm]
        b = flag[rw+1][cm]
    except: c = "#"
    if c == "." and b == 0: dfs(rw+1, cm, flag, mp)
    return flag


    


    
if __name__ == '__main__':
    main()