Problem:
Problem B: Red and Black There is a rectangular room, covered with square tiles. Each tile is
colored either red or black. A man is standing on a black tile.
From a tile, he can move to one of four adjacent tiles. But he can't 
move on red tiles, he can move only on black tiles. Write a program to count the number of black tiles which he can reach 
by repeating the moves described above. Input The input consists of multiple data sets.
 A data set starts with a line containing two positive integers W and H ; W and H are the numbers of tiles in the x -
 and y - directions, respectively. W and H are 
not more than 20. There are H more lines in the data set, each of which
includes W characters. Each character represents the color of a
tile as follows.  '.' - a black tile '#' - a red tile '@' - a man on a black tile(appears exactly once in a data set) The end of the input is indicated by a line consisting of two zeros. Output For each data set, your program 
should output a line which contains the number of 
tiles he can reach from the initial tile (including itself). Sample Input 6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0 Output for the Sample Input 45
59
6
13

Solution (Python):
x=0
y=0
b=[]
t=[]
dir=[[1,0],[0,1],[-1,0],[0,-1]]
ans=0

def dfs(p_x,p_y):
  global b,t,ans
  if t[p_y][p_x] or not b[p_y][p_x]: return
  ans+=1
  t[p_y][p_x]=True
  for i in dir:
    dfs(p_x+i[0],p_y+i[1])

while True:
  x=0
  y=0
  w,h=map(int,input().split())
  if w==0 and h==0: break
  b=[[False]*(w+2) for i in range(h+2)]
  t=[[False]*(w+2) for i in range(h+2)]
  ans=0
  for i in range(1,h+1):
    tmp=list(input())
    for j in range(1,w+1):
      b[i][j]=(tmp[j-1] in {'.','@'})
      if tmp[j-1]=='@': x=j;y=i
  dfs(x,y)
  print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15
Dy = (-1,0,1,0)
Dx = (0,1,0,-1)

from collections import deque,defaultdict,Counter
def solve(H,W):
    grid = [input() for _ in range(H)]
    cnt = [[0]*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if grid[i][j] == '@':
                sy,sx = i,j
                break
    
    q = deque([(sy,sx)])
    cnt[sy][sx] = 1
    ans = 1
    while q:
        ny,nx = q.popleft()
        for dy,dx in zip(Dy,Dx):
            y = ny + dy
            x = nx + dx
            if y < 0 or y >= H:
                continue
            if x < 0 or x >= W:
                continue
            if grid[y][x] == '
                continue
            if cnt[y][x] == 1:
                continue
            cnt[y][x] = 1
            ans += 1
            q.append((y,x))
    print(ans)
    
def main():
    while True:
        W,H = map(int,input().split())
        if H == 0 and W == 0:
            return
        solve(H,W)
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

ans = []
while True:
    w, h = map(int, input().split())
    if not w and not h:
        break
    
    roadmap = []
    d = deque()
    able = [[False for i in range(w)] for j in range(h)]
    for i in range(h):
        roadmap.append(input())
        
    
    for i in range(h):
        for j in range(w):
            if roadmap[i][j] == '@':
                able[i][j] = True
                d.append([i, j])

    
    while len(d):
        y, x = d.pop()
        
        for course in [[0, 1], [1, 0], [-1, 0], [0, -1]]:
            tmpy, tmpx = y + course[0], x + course[1]
            if tmpy < 0 or tmpy >= h or tmpx < 0 or tmpx >= w:
                continue
                
            if not able[tmpy][tmpx] and roadmap[tmpy][tmpx] == '.':
                able[tmpy][tmpx] = True
                d.append([tmpy, tmpx])

    a = 0
    for i in range(h):
        a += len(list(filter(lambda x:x, able[i])))

    ans.append(a)

[print(i) for i in ans]

Solution (Python):
def Check(A, i, j, W, H) :
    if j-1 >= 0 :
        if A[i][j-1] == "." :
            A[i][j-1] = "+"
            Check(A, i, j-1, W, H)
    if i+1 < H :
        if A[i+1][j] == "." :
            A[i+1][j]= "+"
            Check(A, i+1, j, W, H)
    if j+1 < W :
        if A[i][j+1] == "." :
            A[i][j+1] = "+"
            Check(A, i, j+1, W, H)
    if i-1 >= 0 :
        if A[i-1][j] == "." :
            A[i-1][j] = "+"
            Check(A, i-1, j, W, H)
    return(A)

while True :
    W, H = map(int, input().split())
    if W == 0 and H == 0 :
        break
    
    Tile = [list(input()) for i in range(H)]
    for i in range(H) :
        for j in range(W) :
            if Tile[i][j] == "@" :
                my_i = i
                my_j = j
    Tile[my_i][my_j] = "+"
    Tile_new = Check(Tile, my_i, my_j, W, H)
    count = 0
    for i in range(H) :
        for j in range(W) :
            if Tile_new[i][j] == "+" :
                count += 1
    print(count)

Solution (Python):
from collections import deque
def bfs():
    d = [[False] * w for i in range(h)]
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]

    que = deque([])
    que.append((sx,sy))
    d[sx][sy] = True
    cnt = 1

    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            if 0 <= nx < h and 0 <= ny < w and Map[nx][ny] != "
                cnt += 1
                que.append((nx,ny))
                d[nx][ny] = True
    return cnt

while 1:
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    Map = [str(input()) for i in range(h)]
    for i in range(h):
        for j in range(w):
            if Map[i][j] == "@":
                sx = i
                sy = j
    print(bfs())

Solution (Python):
from collections import deque

y = [-1,0,1,0]
x = [0,-1,0,1]

def main():

    h,w = 0,0
    c = []

    def check(i,j):
        return 0<=i and i<h and 0<=j and j<w

    def bfs(a,b):
        res = 0
        d = deque()
        d.append([a,b])
        f = [[False]*w for _ in range(h)]
        while len(d):
            i,j = d.popleft()
            if not check(i,j):continue
            if c[i][j]=='
            if f[i][j]==True:continue
            res += 1
            f[i][j]=True
            for k in range(4):
                d.append([i+y[k],j+x[k]])
        return res

    while True:
        w,h = map(int,input().split())
        if h==0 and w==0:break
        c = []
        for i in range(h):
            c.append(input())
        res = 0
        for i in range(h):
            for j in range(w):
                if c[i][j]=='@':
                    res = bfs(i,j)
        print(res)

if __name__ == '__main__':
    main()

Solution (Python):
dxy = [[1,0],[-1,0],[0,1],[0,-1]]
n=0
def rec(x,y):
    global n
    if (x<0 or x>=h or y<0 or y>=w) or (visited[x][y] or tile[x][y]!='.'):
        return 0
    visited[x][y] = True
    n += 1
    for (dx,dy) in dxy:
        x2 = x + dx
        y2 = y + dy
        rec(x2,y2)
    return 0

w,h=map(int,input().split())
ans = []
while((w,h)!=(0,0)):
    n=0
    tile = []
    for _ in range(h):
        tile.append(list(input()))
    visited = [[False] * w for _ in range(h)]
    for a in range(h):
        for b in range(w):
            if(tile[a][b]=='@'):
                pos_x = a
                pos_y = b
                tile[a][b]='.'
    rec(pos_x, pos_y)
    ans.append(n)
    w,h=map(int,input().split())
for a in ans:
    print(a)

Solution (Python):

'''
所要時間は、分位であった。
'''




import sys
input = sys.stdin.readline






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)]
        
        flag = dfs(sti, stj, flag, mp)
        ans = sum([flag[i].count(1) for i in range(h)])
        print(ans)
        
def dfs(rw, cm, flag, mp):
    flag[rw][cm] = 1
    
    
    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()

Solution (Python):
from collections import deque

while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    else:
        m = []
        d = deque()
        for i in range(h):
            r = input()
            m.append(list(r))
            s = r.find('@')
            if s != -1:
                d.append((s, i))
        while len(d):
            rw, rh = d.popleft()
            m[rh][rw] = 'Y'
            if 0 < rw and m[rh][rw-1] == '.':
                d.append((rw-1, rh))
                m[rh][rw-1] = 'Y'
            if rw < w-1 and m[rh][rw+1] == '.':
                d.append((rw+1, rh))
                m[rh][rw+1] = 'Y'
            if 0 < rh and m[rh-1][rw] == '.':
                d.append((rw, rh-1))
                m[rh-1][rw] = 'Y'
            if rh < h-1 and m[rh+1][rw] == '.':
                d.append((rw, rh+1))
                m[rh+1][rw] = 'Y'
        a = 0
        for i in m:
            a += i.count('Y')
        print(a)

Solution (Python):
dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def rec(maze, visited, locat):
  
  for i in dxy:
    n_locat = (locat[0] + i[0], locat[1] + i[1])
    if 0 <= n_locat[0] < len(maze) and 0 <= n_locat[1] < len(maze[0]):
      if maze[n_locat[0]][n_locat[1]] == '.':
        if visited[n_locat[0]][n_locat[1]] == False:
          global count 
          count += 1
          visited[n_locat[0]][n_locat[1]] = True
          rec(maze, visited, n_locat)
  return count
while(True):
  w, h = map(int, input().split())
  if w == 0 and h == 0:
    break
  maze = [[]for i in range(h)]
  visited = [[False for i in range(w)] for j in range(h)]
  for i in range(h):
    mazel = list(input())
    maze[i] = mazel
  count = 1 
  for i in range(h):
    for j in range(w):
      if maze[i][j] == '@':
        s = [i, j]
        break
    else:
      continue
    break
  print(rec(maze, visited, s))

Solution (Python):
adjR = [-1, 0, 0, 1]
adjC = [0, 1, -1, 0]

def isValid(row, col, ROWS, COLS):
    return row >= 0 and row < ROWS and col >= 0 and col < COLS

def floodfill(board, row, col, ROWS, COLS):
    global adjR
    global adjC

    if not isValid(row, col, ROWS, COLS):
        return 0
    if board[row][col] == '
        return 0

    board[row][col] = '
    total = 1
    for x in range(4):
        rr = row + adjR[x]
        cc = col + adjC[x]
        total += floodfill(board, rr, cc, ROWS, COLS)
    return total
    

if __name__ == '__main__':
    while True:
        COLS, ROWS = list(map(int, input().split()))
        if ROWS == 0 and COLS == 0:
            break

        board = []
        for i in range(ROWS):
            line = input()
            board.append([ c for c in line ])
            if '@' in line:
                row = i
                col = line.find('@')
        
        print(  floodfill(board, row, col, ROWS, COLS)  )

Solution (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)

Solution (Python):
h=0
w=0

while 1:
    w,h=map(int,input().split())
    if h==0 and w==0:
        break
    grid=[]
    sx=-1
    sy=-1
    ans=1
    used=[[0]*w for i in range(h)]
    def valid(i,j):
        return i>=0 and j>=0 and i<h and j<w and grid[i][j]=='.' and not used[i][j]
    for i in range(h):
        row=input()
        grid.append(row)
        for j in range(w):
            if row[j]=='@':
                sx=i
                sy=j
    stk=[(sx,sy)]
    while stk:
        x=stk[-1][0]
        y=stk[-1][1]
        stk.pop()
        for d in [(1,0),(0,1),(-1,0),(0,-1)]:
            xx=x+d[0]
            yy=y+d[1]
            if valid(xx,yy):
                stk.append((xx,yy))
                used[xx][yy]=1
                ans+=1
    print(ans)

Solution (Python):
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    m = []
    for _ in range(h):
        m.append(list(input()))
    visited = [[False] * w for _ in range(h)]
    dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    answer = 0


    def rec(x, y):
        global answer
        visited[x][y] = True
        answer += 1
        for (dx, dy) in dxy:  
            x2 = x + dx
            y2 = y + dy
            if 0 <= x2 < h and 0 <= y2 < w:
                if m[x2][y2] == '.':
                    if not visited[x2][y2]:
                        rec(x2, y2)  


    for i in range(h):
        for j in range(w):
            if m[i][j] == '@':
                rec(i, j)
                break
        else:
            continue
        break
    print(answer)

Solution (Python):
def count_area(h,w):
	count = 0
	if (h>0) and (s[h-1][w]== "."):
		s[h-1][w] = "
		count += count_area(h-1,w)
	if (h<H-1) and (s[h+1][w]== "."):
		s[h+1][w] = "
		count += count_area(h+1,w)
	if (w>0) and (s[h][w-1]== "."):
		s[h][w-1] = "
		count += count_area(h,w-1)
	if (w<W-1) and (s[h][w+1]== "."):
		s[h][w+1] = "
		count += count_area(h,w+1)
	return count+1

while(True):
	W, H = map(int, input().split())
	if H==0&W==0:break

	s = [list(input()) for i in range(H)]
	
	for h in range(H):
		for w in range(W):
			if s[h][w] == "@":
				s[h][w] = "
				print(count_area(h,w))
				break

Solution (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 = []  
    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)

Solution (Python):
from queue import *


d = [(1,0),(0,1),(-1,0),(0,-1)]
temp={}

while True:
    W,H = (int(x) for x in input().split())
    mmap=[]
    visited=[]
    flag=0
    count=1
    
    q = Queue()
    
    if W==0 & H==0:
        break
    for i in range(0,H):
        mmap.append(list(input()))
    for i in range (0,H):
        for j in range(0,W):
            if mmap[i][j] == '@':
                start=[i,j]
    
    q.put(start)
    visited.append(list(start))
    while not q.empty():
        now = q.get()
        for myd in d:
            now2 = list(now)
            now2[0] += myd[0]
            now2[1] += myd[1]
            if not(now2[0]==-1 or now2[0]==H or now2[1]==-1 or now2[1]==W) :
                if mmap[now2[0]][now2[1]] != '
                    flag=0
                    for v in visited:
                        if now2 == v :
                            flag=1
                            break
                    if flag==0:
                        count+=1
                        q.put(now2)
                        visited.append(list(now2))
    
    print(count)

Solution (Python):
from collections import deque

dx,dy = [1,0,-1,0],[0,1,0,-1]

while True:
	w,h = map(int,input().split())
	if w == 0 and h == 0:
		break
	field = [input() for i in range(h)]
	for i in range(h):
		if '@' in field[i]:
			sx,sy = i,field[i].find('@')
	visited = [[False for i in range(w)] for j in range(h)]
	que = deque()
	que.append((sx,sy))
	visited[sx][sy] = True
	while que:
		p = que.popleft()
		for i in range(4):
			nx,ny = p[0] + dx[i],p[1] + dy[i]
			if 0 <= nx < h and 0 <= ny < w and field[nx][ny] != '
				visited[nx][ny] = True
				que.append((nx,ny))
	cnt = 0
	for i in range(h):
		for j in range(w):
			cnt += visited[i][j]
	print(cnt)

Solution (Python):
from collections import defaultdict
def explore(now,cnt):
    if cnt>W*H:
        a=0 
    else:
        d[now]=1
        if now[1]+1<W:
            if not data[now[0]][now[1]+1]=="
                if not (now[0],now[1]+1) in d.keys():
                    explore((now[0],now[1]+1),cnt+1)
        if now[0]+1<H:
            if not data[now[0]+1][now[1]]=="
                if not (now[0]+1,now[1]) in d.keys():
                    explore((now[0]+1,now[1]),cnt+1)
        if now[0]-1>=0:
            if not data[now[0]-1][now[1]]=="
                if not (now[0]-1,now[1]) in d.keys():
                    explore((now[0]-1,now[1]),cnt+1)
        if now[1]-1>=0:
            if not data[now[0]][now[1]-1]=="
                if not (now[0],now[1]-1) in d.keys():
                    explore((now[0],now[1]-1),cnt+1)
    
while True:
    W,H=map(int,input().split())
    if W==0 and H==0:
        break
    data=[list(input()) for _ in range(H)]
    d=defaultdict()
    for i in range(H):
        for j in range(W):
            if data[i][j]=="@":
                start=(i,j)
                break
    now=start
    cnt=0
    explore(now,cnt)
    print(len(d.keys()))

Solution (Python):
while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    maze = []
    for _ in range(H):
        maze.append(list(input()))
    visited = [[False] * W for _ in range(H)]
    dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    ans = 0


    def rec(x, y):
        global ans
        visited[x][y] = True
        ans += 1
        for (dx, dy) in dxy:  
            x2 = x + dx
            y2 = y + dy
            if 0 <= x2 < H and 0 <= y2 < W:
                if maze[x2][y2] == '.':
                    if not visited[x2][y2]:
                        rec(x2, y2)  


    for h in range(H):
        for w in range(W):
            if maze[h][w] == '@':
                rec(h, w)
                break
        else:
            continue
        break

    print(ans)

Solution (Python):
import queue

while True :
    w,h = map(int,input().split())
    if w==0 and h ==0 :
        break
    maze = [input() for _ in range(h)]
    visited = [[-1]*w for _ in range(h)]
    for i in range(h):
        if "@"  in  maze[i]:
            x_f = i
    for i in range(w):
        if maze[x_f][i] =="@":
            y_f = i
    visited[x_f][y_f] = 1
    q = queue.Queue()
    q.put([x_f,y_f])
    cnt  = 1
    while  not q.empty():
        x,y = q.get()
        for dx,dy in [(1,0),(-1,0),(0,1),(0,-1)]:
            if 0<= x+dx < h and 0<= y+dy < w:
                if visited[x+dx][y+dy] == -1 and maze[x+dx][y+dy]=='.':
                    cnt += 1
                    visited[x+dx][y+dy] = 1
                    q.put([x+dx,y+dy])
    print(cnt)

Solution (Python):
from collections import deque

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

while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    
    sx = -1
    sy = -1
    mat = [[None for _ in range(W)] for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == "@":
                sx = j
                sy = i
            mat[i][j] = s[j]
    
    ans = 0
    q = deque()
    q.append((sx, sy))
    while q:
        (cx, cy) = q.popleft()
        ans += 1
        for dx, dy in dxy:
            nx = cx + dx
            ny = cy + dy
            if 0 <= nx < W and 0 <= ny < H:
                if mat[ny][nx] == ".":
                    mat[ny][nx] = "
                    q.append((nx, ny))
    print(ans)

Solution (Python):
import queue
dxy=[(1,0),(0,1),(-1,0),(0,-1)]
def bfs(sx,sy,H,W):
    q=queue.Queue()
    visited=[[False]*W for _ in range(H)]
    q.put((sx,sy))
    visited[sx][sy]=True
    ans=1
    while not q.empty():
        px,py=q.get()
        for dx,dy in dxy:
            nx=px+dx
            ny=py+dy
            if 0<=nx<H and 0<=ny<W:
                if c[nx][ny]=='.' and visited[nx][ny]==False:
                    visited[nx][ny]=True
                    q.put((nx,ny))
                    ans+=1
    return ans
while True:
    W,H=map(int,input().split())
    if(W==0 and H==0):
        break
    c=[input() for _ in range(H)]
    for i in range(H):
        if "@" in c[i]:
            sx=i
            sy=c[i].index("@")
    print(bfs(sx,sy,H,W))

Solution (Python):
from collections import deque
while 1:
    W,H = map(int,input().split())
    if W==0 and H==0:break
    tizu = []
    for i in range(H):
        row = input()
        if "@" in row:
            start = (i,row.index("@"))
        tizu.append(row)
    went = [[False]*W for i in range(H)]
    went[start[0]][start[1]] = True
    ans = 1
    queue = deque()
    queue.append(start)
    while queue:
        curh,curw = queue.popleft()
        for dh,dw in ((0,1),(0,-1),(1,0),(-1,0)):
            newh = curh + dh
            neww = curw + dw
            if 0<=newh<H and 0<=neww<W:
                if tizu[newh][neww] == "." and not went[newh][neww]:
                    went[newh][neww] =True
                    queue.append((newh,neww))
                    ans += 1
    print(ans)

Solution (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)

Solution (Python):
from collections import deque

def solve(W, H, field):
    dire = [(1,0), (0,1), (-1,0), (0,-1)]
    for i in range(H):
        for j in range(W):
            if (field[i][j]=="@"):
                s = (i,j)
    check = [[0]*W for i in range(H)]
    q = deque()
    q.append(s)
    check[s[0]][s[1]] = 1
    ret = 1
    while q:
        y, x = q.popleft()
        for dy, dx in dire:
            if (y+dy<H) and (y+dy>=0) and (x+dx<W) and (x+dx>=0):
                if (field[y+dy][x+dx] == ".") and (not check[y+dy][x+dx]):
                    q.append((y+dy, x+dx))
                    check[y+dy][x+dx] = 1
                    ret += 1
    return ret

def main():
    ans = []
    while True:
        W, H = list(map(int, input().split()))
        if (not W) and (not H):
            break
        field = [input() for i in range(H)]
        ans.append(solve(W, H, field))
    for i in ans:
        print(i)
    
main()

Solution (Python):
def main():
    while 1:
        w, h = map(int, input().split())
        if w == 0: break
        fld = [[] for i in range(h)]
        for i in range(h):
            readline = input()
            for j in range(w):
                item = readline[j]
                fld[i].append(item)
                if item == '@': sy, sx = i, j
        solve(fld, sy, sx)
        count = 0
        for i in range(h):
            for j in range(w):
                if fld[i][j] == 'A': count += 1
        print(count)

def solve(fld, cy, cx):
    vy = [1,-1,0,0]
    vx = [0,0,1,-1]
    fld[cy][cx] = 'A'
    for i in range(4):
        ny = cy + vy[i]
        nx = cx + vx[i]
        if ny < 0 or nx < 0 or ny >= len(fld) or nx >= len(fld[0]):continue
        elif fld[ny][nx] == '.': solve(fld, ny, nx)
main()

Solution (Python):
vec = [[1,0],[0,1],[-1,0],[0,-1]]

def dfs(nw,nh):
    global count
    count += 1
    visited[nh][nw] = True
    for x,y in vec:
        new_w = nw + x
        new_h = nh + y
        if (0 <= new_w) and (new_w) < w:
            if (0 <= new_h) and (new_h) < h:
                if (lis_f[new_h][new_w] == ".") and (visited[new_h][new_w] == False):
                    dfs(new_w,new_h)
                    
while(True):
    w,h = map(int,input().split())
    if (w,h) == (0,0):
        break
    lis_f = []
    for _ in range(h):
        lis_f.append(list(input()))
    
    visited = [[False for _ in range(w)] for _ in range(h)]
    count = 0
    
    id_w = 0
    id_h = 0
    set_w = 0
    set_h = 0
    while(id_h < h):
        while(id_w < w):
            
            if lis_f[id_h][id_w] == "@":
                set_w = id_w
                set_h = id_h
            id_w += 1
        id_h += 1
        id_w = 0
    
    
    
    dfs(set_w,set_h)
    print(count)

Solution (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)] 
        sx,sy = 0,0
        for y in range(H):
            row = list(input())
            if "@" in row:
                sy = y
                sx = row.index("@")
            MMAP[y] = row
        
        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

    
    

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

Solution (Python):
move = [[0, 1], [0, -1], [1, 0], [-1, 0]]
from collections import deque
def main(w, h):
    field = [list(input()) for i in range(h)]
    check = [[True] * w for i in range(h)]
    q = deque()
    for i in range(h):
        for k in range(w):
            if field[i][k] == "@":
                q.append((k, i))
                check[i][k] = False
    ans = 1
    while q:
        x, y = q.popleft()
        for mx, my in move:
            xm = mx + x
            ym = my + y
            if 0 <= xm < w and 0 <= ym < h:
                if field[ym][xm] == "." and check[ym][xm]:
                    check[ym][xm] = False
                    q.append((xm, ym))
                    ans += 1
    print(ans)
while 1:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    main(w,h)

Solution (Python):
while True:
    w,h = map(int,input().split())
    if w == 0:
        break
    around = ''
    for i in range(w + 2):
        around += '
    maze = [around]
    cnt = 1
    for i in range(h):
        row = '
        maze.append(row)
        if '@' in row:
            loc = [[i + 1,row.index('@')]]
    maze.append(around)
    visted = loc
    left = True
    while left:
        next_loc = []
        for x,y in loc:
            for i,j in ([1,0],[0,1],[-1,0],[0,-1]):
                if maze[x + i][y + j] == '.' and [x + i,y + j] not in visted:
                    cnt += 1
                    next_loc.append([x + i,y + j])
                    visted.append([x + i,y + j])
            if  next_loc == []:
                print(cnt)
                left = False
                break
        loc = next_loc

Solution (Python):
from collections import deque

move = ((0,1),(1,0),(0,-1),(-1,0))

def inmap(x,y):
    if 0 <= x <= Lx-1 and 0<= y <= Ly-1:
        return True
    return False

while True:
    Lx,Ly = map(int,input().split())
    if Lx == 0:
        break
    curx= -1
    cury = -1
    orimap = [[] for _ in range(Ly)]
    for i in range(Ly):
        for j in input():
            orimap[i].append(j)
    for y in range(len(orimap)):
        for x in range(len(orimap[y])):
            if orimap[y][x] == "@":
                curx,cury = x,y
                orimap[y][x]="
                break
        if curx != -1:
            break
    Q = deque()
    Q.append([curx,cury])
    count = 1
    while len(Q)>0:
        curx,cury = Q.popleft()
        for dx,dy in move:
            nextx = curx + dx
            nexty = cury + dy
            if inmap(nextx,nexty):
                if orimap[nexty][nextx] != "
                    orimap[nexty][nextx] = "
                    Q.append([nextx,nexty])
                    count += 1
    print(count)

Solution (Python):
import collections
while True:
    w,h = map(int,input().split())
    if w == h == 0:
        break
    else:
        ans = 1
        L = []
        q = collections.deque()
        for i in range(h):
            s = input()
            T = []
            for j in range(w):
                if s[j] == '@':
                    T.append(0)
                    q.append([i,j])
                else:
                    T.append(s[j])
            L.append(T)
        dy_dx = [[1,0],[0,1],[-1,0],[0,-1]]
        while len(q) > 0:
            cur = q.popleft()
            for i in range(4):
                y = cur[0]+dy_dx[i][0]
                x = cur[1]+dy_dx[i][1]
                if 0 <= x < w and 0 <= y < h:
                    if L[y][x] == '.':
                        L[y][x] = L[cur[0]][cur[1]]+1
                        ans += 1
                        q.append([y,x])
        print(ans)

Solution (Python):
while True:
    w, h = map(int, input().split())
    if h == 0:
        break
    m = [list(input()) for _ in range(h)]
    sx = 0
    sy = 0
    for yi, row in enumerate(m):
        if '@' in row:
            sy = yi
            sx = row.index('@')
    def dfs(x, y):
        if x < 0 or w <= x or y < 0 or h <= y or m[y][x] == '
            return 0
        else:
            m[y][x] = '
            dy = [1, -1] + [0] * 2
            dx = [0] * 2 + [1, -1]
            return sum([dfs(x+dx[i], y+dy[i]) for i in range(4)]) + 1
    print(dfs(sx, sy))