Buggy python submission (verdict: Runtime Error):
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)

Fixed submission:
def dfs(tiles, w, h, x, y):
    cnt = 1
    tiles[y][x] = '@'
    for dx,dy in ((0,1), (0,-1), (-1,0), (1,0)):
        if 0<=x+dx<w and 0<=y+dy<h and tiles[y+dy][x+dx]=='.':
            cnt += dfs(tiles, w, h, x+dx, y+dy)
    return cnt

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input()] for h in range(H)]
        for i in range(H):
            if '@' in tiles[i]:
                Y = i
                X = tiles[i].index('@')
        answer = dfs(tiles, W, H, X, Y)
        print(answer)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
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 i<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*W][i]<9999:
            cnt += 1
    print(cnt)

Fixed submission:
def dfs(tiles, w, h, x, y):
    cnt = 1
    tiles[y][x] = '@'
    for dx,dy in ((0,1), (0,-1), (-1,0), (1,0)):
        if 0<=x+dx<w and 0<=y+dy<h and tiles[y+dy][x+dx]=='.':
            cnt += dfs(tiles, w, h, x+dx, y+dy)
    return cnt

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input()] for h in range(H)]
        for i in range(H):
            if '@' in tiles[i]:
                Y = i
                X = tiles[i].index('@')
        answer = dfs(tiles, W, H, X, Y)
        print(answer)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
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 i<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*W][i]<9999:
            cnt += 1
    print(cnt)

Fixed submission:
def dfs(tiles, w, h, x, y):
    cnt = 1
    tiles[y][x] = '@'
    for dx,dy in ((0,1), (0,-1), (-1,0), (1,0)):
        if 0<=x+dx<w and 0<=y+dy<h and tiles[y+dy][x+dx]=='.':
            cnt += dfs(tiles, w, h, x+dx, y+dy)
    return cnt

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input()] for h in range(H)]
        for i in range(H):
            if '@' in tiles[i]:
                Y = i
                X = tiles[i].index('@')
        answer = dfs(tiles, W, H, X, Y)
        print(answer)

if __name__ == '__main__':
    main()