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):
W,H=(int(i) for i in input().split())
while W!=0 or H!=0:
    a=[]
    T=[]
    for i in range(H):
        T.append(input())
        a.append([])
        for j in range(W):
            a[i].append(0)
 
    i=0
    j=0
    while T[i][j]!="@":
        j=0
        while j<W and T[i][j]!="@":
            j+=1
    
        if j>=W:
            j-=1
        if T[i][j]!="@":
            i+=1
  
    s=[[i,j]]
    a[i][j]=1
    c=1
    while s!=[]:
        u=[]
        for l in range(len(s)):
            i=s[l][0]
            j=s[l][1]
            i-=1
            k=0
            while k<2:
            
                if 0<=i<H and a[i][j]==0 and T[i][j]==".":
                    a[i][j]=1
                    u.append([i,j])
                    c+=1
                i+=2
                k+=1
            i-=3
            j-=1
            k=0
            while k<2:
                if 0<=j<W and a[i][j]==0 and T[i][j]==".":
                    a[i][j]=1 
                    u.append([i,j])
                    c+=1
                j+=2
                k+=1
        s=u
    
    print(c)
    W,H=(int(i) for i in input().split())

Solution (Python):


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 = []
        position =(0,0)
        for i in range(h):
            if '@'in data[i]:position = (i,data[i].index('@'))
        stack = []
        count = 0
        stack.append(position)
        while len(stack) !=0:
            position = stack.pop()
            count +=1
            if position[0] -1 >= 0 and not (position[0]-1,position[1])in visited and data[position[0] -1][position[1]] == '.':
                visited.append((position[0] -1,position[1]))
                stack.append((position[0] -1,position[1]))
            if position[1] -1 >= 0 and not (position[0],position[1]-1)in visited and data[position[0]][position[1]-1] == '.':
                visited.append((position[0],position[1]-1))
                stack.append((position[0],position[1]-1))
            if position[0] +1 < h and not (position[0]+1,position[1])in visited and data[position[0] +1][position[1]] == '.':
                visited.append((position[0]+1,position[1]))
                stack.append((position[0]+1,position[1]))
            if position[1] + 1 < w and not (position[0],position[1]+1)in visited and data[position[0]][position[1] +1] == '.':
                visited.append((position[0],position[1] +1))
                stack.append((position[0],position[1]+1))

        print(count)

Solution (Python):


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 = []
        position =(0,0)
        for i in range(h):
            if '@'in data[i]:position = (i,data[i].index('@'))
        stack = []
        count = 0
        stack.append(position)
        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)

Solution (Python):


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)

Solution (Python):


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)

Solution (Python):
import queue

def solve(w, h):
    q = queue.Queue()
    m = [['
    for i in range(h):
        inp = input()
        for j in range(w):
            m[i+1][j+1] = inp[j]
            if m[i+1][j+1] == '@':
                start = [i+1,j+1]
    q.put(start)
    c = 1
    while not q.empty():
        cur = q.get()
        XY = [(1,0), (-1, 0), (0, 1), (0, -1)]
        for x, y in XY:
            cx = cur[0]+x
            cy = cur[1]+y
            if m[cx][cy] == '.':
                m[cx][cy] = '
                q.put([cx, cy])
                c+=1
    return c

while True:
    w, h = [int(i) for i in input().split()]
    if w == 0 and h == 0:
        break
    print(solve(w,h))

Solution (Python):
while 1:
	w,h = map(int, raw_input().split())
	if w == h == 0:
		break
	count = 0
	cnt = 1
	lst = [list(raw_input()) for i in range(h)]
	queue = []
	for i in range(h):
		if "@" in lst[i]:
			wNow, hNow = lst[i].index("@"),i
	queue.append([hNow, wNow])
	while 1:
		hNow,wNow = queue[0][0],queue[0][1]
		queue.pop(0)
		if hNow < h-1:
			if lst[hNow+1][wNow] == ".":
				queue.append([hNow+1,wNow])
				lst[hNow+1][wNow] = "$"
				cnt += 1
		if wNow < w-1:
			if lst[hNow][wNow+1] == ".":
				queue.append([hNow,wNow+1])
				lst[hNow][wNow+1] = "$"
				cnt += 1
		if hNow > 0:
			if lst[hNow-1][wNow] == ".":
				queue.append([hNow-1,wNow])
				lst[hNow-1][wNow] = "$"
				cnt += 1
		if wNow > 0:
			if lst[hNow][wNow-1] == ".":
				queue.append([hNow,wNow-1])
				lst[hNow][wNow-1] = "$"
				cnt += 1
		if len(queue) == 0:
			break
	print cnt

Solution (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] == '
            visited[ny][nx] = 1
            ans += 1
            q.append((nx,ny))
    print(ans)

Solution (Python):
import copy
w, h = map(int,input().split())
while [h, w] != [0, 0]:
    area = []
    a = 1
    for i in range(h):
        area.append(list(input()))
        area[i].insert(0,"
        area[i].append("
    area.insert(0,["
    area.append(["

    que = []
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if area[i][j] == "@":
                que.append([i, j])
                break
        else:
            continue
        break
    while True:
        nque = []
        for i in que:
            h1 = i[0]
            w1 = i[1]
            if area[h1 - 1][w1] == ".":
                area[h1 - 1][w1] = "*"
                nque.append([h1 - 1, w1])
                a += 1
            if area[h1 + 1][w1] == ".":
                area[h1 + 1][w1] = "*"
                nque.append([h1 + 1, w1])
                a += 1
            if area[h1][w1 - 1] == ".":
                area[h1][w1 - 1] = "*"
                nque.append([h1, w1 - 1])
                a += 1
            if area[h1][w1 + 1] == ".":
                area[h1][w1 + 1] = "*"
                nque.append([h1, w1 + 1])
                a += 1
        if nque == []:
            break
        que = copy.deepcopy(nque)

    print(a)
        
    w, h = map(int,input().split())

Solution (Python):
from queue import Queue

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break

    tile = [input() for i in range(h)]
    for x, col in enumerate(tile):
        if '@' in col:
            y = col.index('@')
            s = (x, y)
            break

    visit = [[False for i in range(w)] for j in range(h)]
    dx = (0, -1, 1, 0)
    dy = (1, 0, 0, -1)
    que = Queue()
    que.put(s)
    while not que.empty():
        x, y = que.get()
        if visit[x][y]:
            continue

        visit[x][y] = True
        for j in range(4):
            sx = x + dx[j]
            sy = y + dy[j]
            if sx < 0 or sx >= h or sy < 0 or sy >= w:
                continue
            if tile[sx][sy] != '
                que.put((sx, sy))

    ans = 0
    for col in visit:
        ans += col.count(True)

    print(ans)

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

Solution (Python):



from logging import getLogger, StreamHandler, NullHandler, DEBUG, INFO
logger = getLogger(__name__)

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 == "
            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)

Solution (Python):



from logging import getLogger, StreamHandler, NullHandler, DEBUG, INFO
logger = getLogger(__name__)

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


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

        
        
        queue = [(start_x, start_y)]
        
        while len(queue) > 0:
            x, y = queue.pop()

            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 == "
                    continue
                if (new_x, new_y) in queue:
                    continue
                
                logger.debug("{}, {}".format(new_x, new_y))
                queue.append((new_x, new_y))

        print(g_count)

Solution (Python):
while 1:
	x,y = list(map(int,input().split()))
	if x == 0:
		break

	pp = [1,-1,0,0]
	qq = [0,0,1,-1]
	m = []
	for i in range(y):
		m.append(list(input()))
		if '@' in m[-1]:
			ss = m[-1].index('@')
			tt = i
	cnt = 0

	def move (cx,cy):
		global cnt
		if cy >= y or cy < 0 or cx >= x or cx < 0:
			return
		if m[cy][cx] == '.' or  m[cy][cx] == '@':
			cnt += 1
			m[cy][cx] = '0'
			for i in range(4):
				move(cx + pp[i],cy + qq[i])
	move(ss,tt)
	print(cnt)

Solution (Python):


def search(a,b,dx,dy,t,check,li,answer):
    for i in range(len(dx)):
            temp = [t[0]+dx[i],t[1]+dy[i]]
            if mapCheck(a,b,temp):
                if check[temp[1]][temp[0]] == 0:
                    if li[temp[1]][temp[0]] == '.':
                        answer += 1
                        check[temp[1]][temp[0]] = 1
                        answer = search(a,b,dx,dy,temp,check,li,answer)
    return answer
                    
                
            
def mapCheck(a,b,t):
    if t[0] <= a-1 and t[0] >=0:
        if t[1] <= b-1 and t[1] >= 0:
            return True
    return False

while True:
    li = []
    answer = 1
    start = [0,0]
    a,b = map(int, input().strip().split(' '))
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    if a == 0 and b == 0:
        break
    for i in range(b):
        li.append(input().strip().split())
    check = [[0 for i in range(a)] for j in range(b)]
    for i in range(len(li)):
        li[i] = str(li[i])
        li[i] = list(li[i])
        del li[i][0]
        del li[i][0]
        del li[i][len(li[i])-1]
        del li[i][len(li[i])-1]
        for j in range(len(li[i])):
            if li[i][j] == '@':
                start = [j,i]
    print(search(a,b,dx,dy,start,check,li,answer))

Solution (Python):
def fill(y,x):
    global l,c,w,h
    if 0<=x+1<w and 0<=y<h and l[y][x+1]==".":
        c+=1
        l[y][x+1]=1
        fill(y,x+1)
    if 0<=x-1<w and 0<=y<h and l[y][x-1]==".":
        c+=1
        l[y][x-1]=1
        fill(y,x-1)
    if 0<=x<w and 0<=y+1<h and l[y+1][x]==".":
        c+=1
        l[y+1][x]=1
        fill(y+1,x)
    if 0<=x<w and 0<=y-1<h and l[y-1][x]==".":
        c+=1
        l[y-1][x]=1
        fill(y-1,x)
    return c

while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[list(input()) for _ in range(h)]
    y,x=[[n,i.index("@")] for (n,i) in enumerate(l) if "@" in i][0]
    l[y][x]=1
    c=1
    print(fill(y,x))

Solution (Python):


h,w=1,1

count=0

def search(x,y):
    
    
    global count
    if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]=='
 
        return
        
    
    
    if reached[y][x]:
         return

    reached[y][x]=1
    count+=1
    



    search(x+1,y)
    search(x-1,y)
    search(x,y+1)
    search(x,y-1)
    



while h!=0 and w!=0: 

    count=0
    

    w,h=map(int ,raw_input().split())
    if h==0 and w==0:
        break

    map_data = [[0]*w for _ in range(h)]
    reached = [[0]*w for _ in range(h)]
    for i in range(h):
        ct = raw_input()
        
        for j in range(w):
            map_data[i][j] = ct[j]
            if ct[j]=="@":
                sx=j
                sy=i

    



    search(sx,sy)

    print(count)
    count=0
    w=1
    h=1

Solution (Python):


h,w=1,1

count=0

def search(x,y):
    
    
    global count
    if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]=='
 
        return
        
    
    
    if reached[y][x]:
         return

    reached[y][x]=1
    count+=1
    



    search(x+1,y)
    search(x-1,y)
    search(x,y+1)
    search(x,y-1)
    



while h!=0 and w!=0: 

    count=0
    

    w,h=map(int ,raw_input().split())
    if h==0 and w==0:
        break

    map_data = [[0]*w for _ in range(h)]
    reached = [[0]*w for _ in range(h)]
    for i in range(h):
        ct = raw_input()
        
        for j in range(w):
            map_data[i][j] = ct[j]
            if ct[j]=="@":
                sx=j
                sy=i

    



    search(sx,sy)

    print(count)
    count=0
    w=1
    h=1

Solution (Python):
def walk(x, y):
        
        global s
        global c
        global v
        if 0 <= x < w and 0 <= y < h:
                if v[y][x] is False:
                        if s[y][x] == '@' or s[y][x] == '.':
                                v[y][x] = True
                                c += 1
                                
                                walk(x-1, y)
                                walk(x, y-1)
                                walk(x+1, y)
                                walk(x, y+1)

while 1:
        c = 0
        w,h = map(int, raw_input().split())
        if w == 0 and h == 0: break
        s = ['' for i in xrange(h)]
        v = [[False for j in xrange(w)] for i in xrange(h)]
        for i in xrange(h):
                s[i] = raw_input()
        for i in xrange(w):
                for j in xrange(h):
                        if s[j][i] == '@':
                                walk(i, j)
        
        print c

Solution (Python):
import sys
import collections

x = [1, 0, -1, 0]
y = [0, 1, 0, -1]
while True:
    W, H = map(int, sys.stdin.readline().split())
    if W == 0:
        break
    
    tile = [[0 for i in xrange(W + 2)] for j in xrange(H + 2)]
    
    visited = set()
    next = set()
    
    for i in xrange(H):
        line = raw_input().strip()
        for j in xrange(W):
            if line[j] == '.':
                tile[i + 1][j + 1] = 1
            if line[j] == '@':
                tile[i + 1][j + 1] = 1
                next.add((i + 1, j + 1))


    while len(next) > 0:
        temp_next = set()
        for n in next:
            visited.add(n)
            for i in xrange(4):
                temp = (n[0] + x[i], n[1] + y[i])
                if temp not in visited and tile[temp[0]][temp[1]] == 1:
                    temp_next.add(temp)
        next = temp_next
    print len(visited)

Solution (Python):
mv=((0,1),(0,-1),(1,0),(-1,0))

def dfs(y,x):
    global cnt
    L[y][x]=""
    cnt+=1
    for i,j in mv:
        mx=x+i
        my=y+j
        if 0<=mx<W and 0<=my<H and L[my][mx]==".":
            dfs(my,mx)

while True:
    W,H=map(int,raw_input().split())
    if W==H==0:break
    cnt=0
    L=[list(raw_input()) for i in range(H)]
    for i in range(H):
        for j in range(W):
            if L[i][j]=="@":
                x,y=j,i
    dfs(y,x)
    print cnt

Solution (Python):
dx = [-1,0,1,0]; dy = [0,-1,0,1];
while 1:
    w,h = map(int, raw_input().split())
    if w==h==0: break;
    m = []; s = []; sx = sy = 0;
    m.append('
    for i in range(h):
        m.append("
    m.append('
    f = [[0 for i in range(w+2)] for j in range(h+2)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            if m[i][j]=='@':
                sx = j; sy = i;
    s.append([sx,sy]);
    f[sy][sx] = 1; ans = 1;
    while len(s)>0:
        x,y = s.pop(0)
        for k in range(4):
            if m[y+dy[k]][x+dx[k]]!='
                s.append([x+dx[k],y+dy[k]])
                f[y+dy[k]][x+dx[k]] = 1; ans += 1;
    print ans

Solution (Python):


count = 0
def dfs(i, j, island):
    global count
    
    dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    
    if i < 0 or i >= len(island) or j < 0 or j >= len(island[0]):
        return

    if island[i][j] == '
        return

    island[i][j] = 'x'
    count += 1
    
    for d in dxy:
        dfs(i+d[0], j+d[1], island)

def solve(w, h, island):
    s_i = 0
    s_j = 0
    for i, row in enumerate(island):
        for j, s in enumerate(row):
            if s == '@':
                s_i = i
                s_j = j
                break

    dfs(s_i, s_j, island)
    print count

def func():
    '''

    '''
    while True:
        global count
        wh = map(int, raw_input().split())
        w = wh[0]
        h = wh[1]
        if w == 0 and h == 0:
            break
        island = []
        count = 0
        for i in range(h):
            row = []
            line = raw_input()
            for j in line:
                row.append(j)
            island.append(row)
        
        
        
        
        

        solve(w, h, island)


func()

Solution (Python):
def solve(w,h):
	if 0 <= w < W and 0 <= h < H and tile[h][w] == ".":
		tile[h][w] = 0
		solve(w+1,h); solve(w,h+1); solve(w-1,h); solve(w,h-1)
while 1:
	W,H = map(int,raw_input().split())
	if W == 0: break
	tile = [list(raw_input()) for i in range(H)]
	for h in range(H):
		try:
			sw = tile[h].index("@")
			sh = h
			tile[sh][sw] = "."
			break
		except:
			pass
	solve(sw,sh)
	print sum(tile[h].count(0) for h in range(H))

Solution (Python):
"""
1. スタート地点の座標を取得
2. スタート地点からbfsをして到達可能な”.”の数を数える
"""

from collections import deque

def bfs(h, w, f, s):
    count = 1
    q = deque()
    q.append(s)

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

    while q:
        i, j = q.popleft()

        for di, dj in move:
            ni , nj = i + di, j + dj
            if 0 <= ni < w and 0 <= nj < h and f[ni][nj] == ".":
                count += 1
                q.append((ni, nj))
                f[ni][nj] = "

    return count

def search_atmark(h, w, f):
    for i in range(h):
        for j in range(w):
            if f[i][j] == "@":
                return (i, j)

def solve(h, w, f):
    s = search_atmark(w, h, f)
    return bfs(h, w, f, s)

if __name__ == '__main__':
    while(True):
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break

        f = [list(input()) for i in range(h)]
        print(solve(w, h, f))

Solution (Python):

import sys

sys.setrecursionlimit(10000000)


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




while True:
    
    width,hight = map(int,input().split())
    if width ==0 :
        break
    d =[]
    for _ in range(hight):
        d.append(list(input()))
    
    def dfs(x, y):
    
        d[y][x]="d"

    
    
        for i in range(4):
            ny = y + dx[i]
            nx = x + dy[i]

        
            if 0 <= ny and ny < hight \
              and 0 <= nx and nx < width \
              and d[ny][nx] == ".":
                dfs(nx,ny) 

    


    for y in range(hight):
        for x in range(width):
            if d[y][x] == "@":
                dfs(x,y)
    
    sum = 0 
    for _ in range(hight):
        sum = sum + d[_].count('d')
    print(sum)

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

Solution (Python):
def solve(w,h,tile):
    
    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])
    return count

while True:
    try:
        w, h = map(int, input().split())
        if w == 0:
            break
        tile = [""] * h
        for i in range(h):
            tile[i] = input()

        print(solve(w,h,tile))
    except:
        break

Solution (Python):

def solve(w,h,tile):
    
    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])
    return count

while True:
    try:
        w, h = map(int, input().split())
        if w == 0:
            break
        tile = [""] * h
        for i in range(h):
            tile[i] = input()

        print(solve(w,h,tile))
    except:
        break

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

    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)

Solution (Python):
import collections

dx = [-1,0,1,0]
dy = [0,-1,0,1]
def movable(dst):
    if dst[0]<0 or dst[0]>b-1:
        return False
    elif dst[1]<0 or dst[1]>a-1:
        return False
    elif MM[dst[0]][dst[1]] == '
        return False
    else:
        return True

while True:
    c = 1
    a,b = map(int,input().split())
    if a==0 and b==0:
        exit()
    else:
        MM = []
        for i in range(b):
            q = list(input())
            MM.append(q)
            if '@' in q:
                st = [i, q.index('@')]

        D = [[-1 for _ in range(a)] for _ in range(b)]
        D[st[0]][st[1]] = 0 
        Q = collections.deque()
        Q.append(st) 
        while len(Q) > 0:
            
            cur = Q.popleft()
            for dir in range(4):
                
                dst = [cur[0]+dx[dir], cur[1]+dy[dir]]
                
                if movable(dst) and D[dst[0]][dst[1]] == -1:
                    D[dst[0]][dst[1]] = D[cur[0]][cur[1]]+1
                    c += 1
                    Q.append(dst) 
        print(c)