Problem p03436 (python)

Fixed submission:
from Queue import Queue
d = [0, 1, 0, -1]
h, w = map(int, raw_input().split())
m = list()
m.append(["#" for _ in xrange(w+2)])
for _ in xrange(h):
    m.append(["#"]+list(raw_input())+["#"])
m.append(["#" for _ in xrange(w+2)])
dat = len(filter(lambda x:x==".", [i for j in m for i in j]))

q = Queue()
q.put((1,1,1))

while not q.empty():
    xy = q.get()
    if not m[xy[0]][xy[1]] == ".":
        continue
    m[xy[0]][xy[1]] = xy[2]
    for i in xrange(4):
        e = (xy[0]+d[i], xy[1]+d[i+1&3], xy[2]+1)
        if m[xy[0]+d[i]][xy[1]+d[i+1&3]] == ".":
            q.put(e)

try:
    print dat-m[h][w]
except:
    print -1

Buggy submission (verdict: Runtime Error):
d = [0, 1, 0, -1]
def f(x, y, n, m):
    if m[x][y] == "#":
        return
    if type(m[x][y]) == type(int()) and m[x][y] <= n:
        return
    if m[x][y] == ".":
        m[x][y] = n
        for i in xrange(4):
            f(x+d[i], y+d[i+1&3], n+1, m)

h, w = map(int, raw_input().split())
m = list()
m.append(["#" for _ in xrange(w+2)])
for _ in xrange(h):
    m.append(["#"]+list(raw_input())+["#"])
m.append(["#" for _ in xrange(w+2)])
dat = len(filter(lambda x:x==".", [i for j in m for i in j]))
f(1, 1, 1, m)
try:
    print dat-m[h][w]
except:
    print -1
for i in m:
    for j in i:
        try:
            print "%2d" % j,
        except:
            print " %s" % j,
    print

Buggy submission (verdict: Runtime Error):
d = [0, 1, 0, -1]
def f(x, y, n, m):
    if m[x][y] == "#":
        return
    if type(m[x][y]) == type(int()) and m[x][y] <= n:
        return
    if m[x][y] == ".":
        m[x][y] = n
        for i in xrange(4):
            f(x+d[i], y+d[i+1&3], n+1, m)

h, w = map(int, raw_input().split())
m = list()
m.append(["#" for _ in xrange(w+2)])
for _ in xrange(h):
    m.append(["#"]+list(raw_input())+["#"])
m.append(["#" for _ in xrange(w+2)])
dat = len(filter(lambda x:x==".", [i for j in m for i in j]))
f(1, 1, 1, m)
try:
    print dat-m[h][w]
except:
    print -1
for i in m:
    for j in i:
        try:
            print "%2d" % j,
        except:
            print " %s" % j,
    print

Buggy submission (verdict: Runtime Error):
d = [0, 1, 0, -1]
def f(x, y, n, m):
    if m[x][y] == "#" or m[x][y] == " ":
        return
    if m[x][y] == "." or m[x][y] > n:
        m[x][y] = n
        for i in xrange(4):
            f(x+d[i], y+d[i+1&3], n+1, m)

h, w = map(int, raw_input().split())
m = list()
m.append([" " for _ in xrange(w+2)])
for _ in xrange(h):
    m.append([" "]+list(raw_input())+[" "])
m.append([" " for _ in xrange(w+2)])
dat = len(filter(lambda x:x==".", [i for j in m for i in j]))
f(1, 1, 1, m)
try:
    print dat-m[h][w]
except:
    print -1

Buggy submission (verdict: Runtime Error):
d = [0, 1, 0, -1]
def f(x, y, n, m):
    if m[x][y] == "#" or m[x][y] == " ":
        return
    if m[x][y] == "." or m[x][y] > n:
        m[x][y] = n
        for i in xrange(4):
            f(x+d[i], y+d[i+1&3], n+1, m)

h, w = map(int, raw_input().split())
m = list()
m.append([" " for _ in xrange(w+2)])
for _ in xrange(h):
    m.append([" "]+list(raw_input())+[" "])
m.append([" " for _ in xrange(w+2)])
dat = len(filter(lambda x:x==".", [i for j in m for i in j]))
f(1, 1, 1, m)
print dat-m[h][w]