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):
class dfs:
    def __init__(self,W,H,m):
        self.W=W
        self.H=H
        self.m=m
        self.ans=0
        self.visited=[[False]*W for _ in range(H)]
    def trav(self,x,y):
        
        self.visited[x][y]=True
        self.ans+=1
        offset=[(-1,0),(0,-1),(1,0),(0,1)]
        for dx,dy in offset:
            x2=x+dx
            y2=y+dy
            if 0<=x2<self.H and 0<=y2<self.W and self.m[x2][y2]=='.' and self.visited[x2][y2]==False:
                self.trav(x2,y2)
    

while True:
    W,H=map(int,input().split())
    if not (W and H): break
    m=[]
    for i in range(H):
        L=list(input())
        if '@' in L:
            (sx,sy)=(i,L.index('@'))
        m.append(L)
    D=dfs(W,H,m)
    D.trav(sx,sy)
    print(D.ans)

Solution (Python):
while True:
    w, h = map(int, input().split())
    if w == 0:
        break
    
    tile = [""] * h
    for i in range(h):
        tile[i] = input()
    
    ok = False
    for y in range(h):
        for x in range(w):
            if tile[y][x] == "@":
                s_x = x
                s_y = y
                ok = True
                break
        if ok:
            break
    
    count = 0
    check = [[s_x, s_y]]
    check_map = [[False for j in range(w)] for i in range(h)]
    while len(check) > 0:
        x, y = check.pop(0)
        if x in range(w) and y in range(h):
            if check_map[y][x] == False:
                if tile[y][x] == "." or tile[y][x] == "@":
                    check_map[y][x] = True
                    count += 1
                    check.append([x, y-1])
                    check.append([x, y+1])
                    check.append([x+1, y])
                    check.append([x-1, y])
    print(count)

Solution (Python):
from collections import deque
d = [(1,0),(-1,0),(0,1),(0,-1)]
while 1:
    w,h = map(int, input().split())
    if w == h == 0:
        break
    s = [input() for i in range(h)]
    bfs_map = [[1 for i in range(w)] for j in range(h)]
    for y in range(h):
        for x in range(w):
            if s[y][x] == "@":
                ans = 1
                q = deque()
                q.append((y,x))
                bfs_map[y][x] = 0
                while q:
                    y, x = q.popleft()
                    for dy,dx in d:
                        y_ = y+dy
                        x_ = x+dx
                        if 0 <= y_ < h and 0 <= x_ < w:
                            if bfs_map[y_][x_] and s[y_][x_] == ".":
                                ans += 1
                                bfs_map[y_][x_] = 0
                                q.append((y_,x_))
                print(ans)
                break

Solution (Python):


from collections import deque

def bfs():
    while 1:
        W,H = map(int,input().split())
        if W == 0 and H == 0:
            break
        maze = [list(input()) for _ in range(H)]
        check = [[False for _ in range(W)] for _ in range(H)]
        
        dx = [1,0,-1,0]
        dy = [0,1,0,-1]
        for i in range(H):
            for j in range(W):
                if maze[i][j] == '@':
                    sx,sy = j,i
                    check[i][j] = True

        ans = 1
        que = deque()
        que.append((sx,sy))
        while que:
            x,y = que.pop()
            for i in range(4):
                nx,ny = x + dx[i],y + dy[i]

                if 0 <= nx < W and 0 <= ny < H and maze[ny][nx] == '.' and check[ny][nx] == False:
                    que.append((nx,ny))
                    check[ny][nx] = True
                    ans += 1
        print(ans)
    return

if __name__ == '__main__':
    bfs()

Solution (Python):
def f(h,w):
  global W,H,S,A
  if h<0 or h>=H or w<0 or w>=W or S[h][w]=="
    return
  A[h][w] = 1
  f(h-1,w)
  f(h+1,w)
  f(h,w-1)
  f(h,w+1)
while True:
  W,H = map(int,input().split())
  if W+H == 0:
    break
  S = [list(input()) for h in range(H)]
  A = [[0 for w in range(W)] for h in range(H)]
  for h in range(H):
    for w in range(W):
      if S[h][w] == "@":
        f(h,w)
        break
  ans = 0
  for i in A:
    ans += sum(i)
  print(ans)

Solution (Python):
c = 0
def calc(t, X, Y):
    global c
    c += 1
    t[Y][X] = '
    for x, y in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        if t[Y + y][X + x] == '.':
            calc(t, X + x, Y + y)
            
while True:
    c = 0
    w, h = map(int, input().split())
    if w == h == 0:
        break
    
    t = [['
    for sy in range(h + 2):
        if '@' in t[sy]:
            sx = t[sy].index('@')
            break
      
    calc(t, sx, sy)
    print(c)

Solution (Python):
import sys

sys.setrecursionlimit(10000)


def dfs(x, y):
    global cout
    if 0 <= x < H - 1 and tile[x + 1][y] == ".":
        cout += 1
        tile[x + 1][y] = "
        dfs(x + 1, y)
    if 0 < x < H and tile[x - 1][y] == ".":
        cout += 1
        tile[x - 1][y] = "
        dfs(x - 1, y)
    if 0 <= y < W - 1 and tile[x][y + 1] == ".":
        cout += 1
        tile[x][y + 1] = "
        dfs(x, y + 1)
    if 0 < y < W and tile[x][y - 1] == ".":
        cout += 1
        tile[x][y - 1] = "
        dfs(x, y - 1)
    return 0


def search_start():
    for i in range(H):
        for j in range(W):
            if tile[i][j] == "@":
                tile[i][j] = "
                return (i, j)


if __name__ == "__main__":
    while True:
        W, H = map(int, input().split())
        if W == 0 and H == 0:
            break
        
        tile = [list(input()) for _ in range(H)]
        cout = 1
        start = search_start()
        
        dfs(start[0], start[1])
        
        
        print(cout)

Solution (Python):
def dfs(now,origin,drawn): 
    positive = '.' 
    negative = '
    direct = [0,0,0,0] 
    height = len(origin)
    width = len(origin[0])
    now_h = now[0]
    now_w = now[1]
    
    if now_h != 0 and origin[now_h-1][now_w] == positive and drawn[now_h-1][now_w] == 0:
        direct[0] = 1
    if now_h != height-1 and origin[now_h+1][now_w] == positive and drawn[now_h+1][now_w] == 0:
        direct[1] = 1
    if now_w != 0 and origin[now_h][now_w-1] == positive and drawn[now_h][now_w-1] == 0:
        direct[2] = 1
    if now_w != width-1 and origin[now_h][now_w+1] == positive and drawn[now_h][now_w+1] == 0:
        direct[3] = 1

    if sum(direct) == 0: 
        return 0
    d = {0:[now_h-1,now_w],1:[now_h+1,now_w],2:[now_h,now_w-1],3:[now_h,now_w+1]} 
    for i in range(4):
        if direct[i] == 1:
            drawn[d[i][0]][d[i][1]] = 1 
            dfs(d[i],origin,drawn) 
    return 0

while True:
    W, H = map(int,input().split()) 
    if W == 0 and H == 0: 
        break
    tiles = [list(input()) for i in range(H)] 
    possible_tiles = [[0 for i in range(W)] for j in range(H)] 
    for h in range(H):
        for w in range(W):
            if tiles[h][w] == '@': 
                now = [h,w]
                break
    possible_tiles[now[0]][now[1]] = 1 
    dfs(now,tiles,possible_tiles) 
    ans = 0
    for h in range(H):
        for w in range(W):
            if possible_tiles[h][w] == 1:
                ans += 1
    print(ans)

Solution (Python):
w,h=map(int,input().split())
while w!=0 or h!=0:
    maps=[]
    flag=False
    ans=0
    for i in range(h):
        k=list(input())
        maps.append(k)
    for i in range(h):
        if flag:
            break
        for j in range(w):
            if maps[i][j]=='@':
                maps[i][j]='
                pos_x=i
                pos_y=j
                flag=True
                break
    def check(x,y):
        global maps
        if 0<=x<h and 0<=y<w:
            pass
        else:
            return False
        if maps[x][y]=='
            return False
        else:
            return True
    def bfs(x,y):
        global ans
        global maps
        for i in [[0,1],[0,-1],[1,0],[-1,0]]:
            p=i[0]
            q=i[1]
            if check(x+p,y+q):
                ans+=1
                maps[x+p][y+q]='
                bfs(x+p,y+q)
    bfs(pos_x,pos_y)
    print(ans+1)
    w,h=map(int,input().split())

Solution (Python):
import math

def search(w,h,data): 
    for i in range(h):
        if '@' in data[i]:
            stx,sty = i,data[i].index('@')

    stack = [[stx,sty]]
    check = [[stx,sty]]
    while True:
        if len(stack) != 0:
            s = stack[-1]
            Hnow,Wnow = s[0],s[1]
        else:
            return len(check)

        stack.pop()
        
        if Hnow < h-1:
            if (data[Hnow+1][Wnow] == '.') and ([Hnow+1,Wnow] not in check):
                stack.append([Hnow+1,Wnow])
                check.append([Hnow+1,Wnow])

        if Wnow < w-1:
            if (data[Hnow][Wnow+1] == '.') and ([Hnow,Wnow+1] not in check):
                stack.append([Hnow,Wnow+1])
                check.append([Hnow,Wnow+1])

        if Hnow > 0:
            if (data[Hnow-1][Wnow] == '.') and ([Hnow-1,Wnow] not in check):
                stack.append([Hnow-1,Wnow])
                check.append([Hnow-1,Wnow])

        if Wnow > 0:
            if (data[Hnow][Wnow-1] == '.') and ([Hnow,Wnow-1] not in check):
                stack.append([Hnow,Wnow-1])
                check.append([Hnow,Wnow-1])

a = []


while True:
    w,h = list(map(int,input().split()))
    if (w == 0)and(h == 0):
        break
    else:
        data = [list(input()) for l in range(h)]
        a.append(search(w,h,data))

for i in a:
    print(i)

Solution (Python):

def check(p):
    global ans
    global field
    ans+=1
    if(field[p[1]][p[0]]=='.'):
        field[p[1]][p[0]]='
    if(0<=p[0]+1<w and field[p[1]][p[0]+1]=='.'):
        check((p[0]+1,p[1]))
    if(0<=p[0]-1<w and field[p[1]][p[0]-1]=='.'):
        check((p[0]-1,p[1]))
    if(0<=p[1]+1<h and field[p[1]+1][p[0]]=='.'):
        check((p[0],p[1]+1))
    if(0<=p[1]-1<h and field[p[1]-1][p[0]]=='.'):
        check((p[0],p[1]-1))
    
    
while 1:
    ans=0
    w,h=map(int,input().split())
    if w==0:
        break
    field=[]
    for i in range(h):
        s=input()
        t=s.find('@')
        if(t!=-1):
            st=(t,i)
        field.append(list(s))
    check(st)
    print(ans)

Solution (Python):
def solve(dat):
    H = len(dat)
    W = len(dat[0])

    for i in range(H):
        for j in range(W):
            if dat[i][j] == "@":
                si, sj = i, j

    tmp = [[0 for _ in range(W)] for _ in range(H)]
    di = [-1,1,0,0]
    dj = [0,0,-1,1]
    pool = [(si, sj)]

    while len(pool) > 0:
        
        i, j = pool.pop()
        tmp[i][j] = 1
        for k in range(4):
            ni = i + di[k]
            nj = j + dj[k]
            if ni in range(H) and nj in range(W) and dat[ni][nj] == "." and tmp[ni][nj] == 0:
                pool.append((ni, nj))
    cnt = sum([sum(tmp[i]) for i in range(H)])
    return cnt


while True:
    W, H = map(int, raw_input().split(" "))
    if W == H == 0:
        break
    dat = [raw_input() for _ in range(H)]
    print solve(dat)