Problem:


Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.  

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
 :   :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209



Sample input:
3 3
..#
#..
...


Sample output:
2

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
G=[]
for _ in range(h):
  s=input()
  S=[]
  for i in range(w):
    S.append(s[i])
  G.append(S)

si,sj=0,0
gi,gj=h-1,w-1
INF=float('inf')
dist=[[INF for i in range(w)] for j in range(h)]
dist[si][sj]=1
Q=deque()
Q.append((si,sj))
while Q:
  pi,pj=Q.popleft()
  for di,dj in [[-1,0],[1,0],[0,-1],[0,1]]:
    ni,nj=pi+di,pj+dj
    if 0>ni or ni>h-1:
      continue
    if 0>nj or nj>w-1:
      continue
    if G[ni][nj]=='#':
      continue
    if dist[ni][nj]>dist[pi][pj]+1:
      dist[ni][nj]=dist[pi][pj]+1
      Q.append((ni,nj))
ans=0      
for i in range(h):
  for j in range(w):
    if G[i][j]=='.':
      ans+=1
ans-=dist[gi][gj]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
G=[]
for _ in range(h):
  s=input()
  S=[]
  for i in range(w):
    S.append(s[i])
  G.append(S)

si,sj=0,0
gi,gj=h-1,w-1
INF=float('inf')
dist=[[INF for i in range(w)] for j in range(h)]
dist[si][sj]=1
Q=deque()
Q.append((si,sj))
while Q:
  pi,pj=Q.popleft()
  for di,dj in [[-1,0],[1,0],[0,-1],[0,1]]:
    ni,nj=pi+di,pj+dj
    if 0>ni or ni>h-1:
      continue
    if 0>nj or nj>w-1:
      continue
    if G[ni][nj]=='#':
      continue
    if dist[ni][nj]==INF:
      dist[ni][nj]=dist[pi][pj]+1
      Q.append((ni,nj))
ans=0      
for i in range(h):
  for j in range(w):
    if G[i][j]=='.':
      ans+=1
ans-=dist[gi][gj]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
G=[]
for _ in range(h):
  s=input()
  S=[]
  for i in range(w):
    S.append(s[i])
  G.append(S)

si,sj=0,0
gi,gj=h-1,w-1
INF=float('inf')
dist=[[INF for i in range(w)] for j in range(h)]
dist[si][sj]=1
Q=deque()
Q.append((si,sj))
while Q:
  pi,pj=Q.popleft()
  for di,dj in [[-1,0],[1,0],[0,-1],[0,1]]:
    ni,nj=pi+di,pj+dj
    if 0>ni or ni>h-1:
      continue
    if 0>nj or nj>w-1:
      continue
    if dist[ni][nj]==INF:
      dist[ni][nj]=dist[pi][pj]+1
      Q.append((ni,nj))
ans=0      
for i in range(h):
  for j in range(w):
    if G[i][j]=='.':
      ans+=1
ans-=dist[gi][gj]
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    from collections import deque

    H, W = map(int, input().split())
    S = []
    num_path = 0
    for _ in range(H):
        s = input()
        num_path += s.count('.')
        S.append(s)

    move = ((0, 1), (0, -1), (1, 0), (-1, 0))
    distance = [[1] * W for _ in range(H)]
    queue = deque([(0, 0)])
    while queue:
        y, x = queue.popleft()
        if y == H - 1 and x == W - 1:
            break
        for dy, dx in move:
            ny = y + dy
            nx = x + dx
            if (1
                    and 0 <= ny < H
                    and 0 <= nx < W
                    and distance[ny][nx] == 1
                    and S[ny][nx] == '.'):
                queue.append((ny, nx))
                distance[ny][nx] = distance[y][x] + 1
    print(num_path - distance[H - 1][W - 1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
h,w=map(int,input().split())
ss=[list(input()) for i in range(h)]
num=0
for i in range(h):
    num+=ss[i].count('.')

visited=[[0 for i in range(w)] for j in range(h)]
queue=[]
queue.append([0,0])
ss[0][0]='fin'
dy=[1,0,-1,0]
dx=[0,1,0,-1]


while len(queue)>0:
    now=queue.pop(0)
    for i in range(4):
        x= now[1]+dx[i]
        y= now[0]+dy[i]
        if 0<=x<w and 0<=y<h :
            if ss[y][x]=='.' and visited[y][x]==0:
                visited[y][x]=visited[now[0]][now[1]]+1
                queue.append([y,x])
            if x==w-1 and y==h-1:
                print(num-visited[y][x]-1)
                sys.exit()

Submission status: Wrong Answer

Solution code:
# 白だけ動いて(0,0)から(H-1, W-1)に行く
# その際にできるだけ黒を増やす
# 全体の白から最短距離を除いたものが答え

from collections import deque


H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)] # .は白、#は黒
visited = [[-1] * W for _ in range(H)]
moves = [(1, 0), (0, 1), (-1, 0), (0, -1)]

q = deque()
q.append((0, 0))
visited[0][0] = 0

while q:
    y, x = q.popleft()

    for dy, dx in moves:
        moved_y = y + dy
        moved_x = x + dx

        if moved_y < 0 or H-1 < moved_y or moved_x < 0 or W-1 < moved_x:
            continue
        if visited[moved_y][moved_x] != -1:
            continue
        visited[moved_y][moved_x] = visited[y][x] + 1
        q.append((moved_y, moved_x))

min_route = visited[H-1][W-1]

if min_route == -1:
    answer = -1
else:
    total_white = 0
    for row in grid:
        total_white += row.count('.')

    answer = total_white - min_route - 1

print(answer)

Submission status: Wrong Answer

Solution code:
import copy
h,w =map(int,input().split())

s = [[]*w for _ in range(h)]
white =0
for i in range(h):
    t = input()
    for j in range(w):
        if t[j] == ".":
            white += 1
            s[i].append(10**5)
        elif t[j] == "#":
            s[i].append(t[j])
now = [0,0]
steps =[]
steps.append(now)
direction = []
s[0][0] =0
a = [2]
while len(steps) >0:
    now = copy.copy(steps[0])
    if s[now[0]][now[1]] =="#":
        continue
    for k in [[0,1],[1,0],[-1,0],[0,-1]]:
        now = copy.copy(steps[0])
        if now[0] + k[0] >=0 and now[0] + k[0] < h:
            now[0] += k[0]
            if now[1] + k[1] >=0 and now[1] + k[1] <w:
                now[1] += k[1]
                if s[now[0]][now[1]] == 10**5:
                    if not [now[0],now[1]] in steps:
                        steps.append([now[0],now[1]])
            else:
                continue
        else:
            continue
    # if s[steps[0][0]][steps[0][1]] ==0:
    #     steps.pop(0)
    #     continue
    if s[steps[0][0]][steps[0][1]] >10**4:
        direction =[]
        for l in [[0,1],[1,0],[-1,0],[0,-1]]:
            if steps[0][0]+l[0]>=0 and steps[0][0]+l[0]<h and steps[0][1]+l[1]< w and steps[0][1]+l[1] >=0 and s[steps[0][0]+l[0]][steps[0][1]+l[1]] != "#":
                s[steps[0][0]][steps[0][1]]=min(s[steps[0][0]+l[0]][steps[0][1]+l[1]]+1,s[steps[0][0]][steps[0][1]])
    steps.pop(0)
if s[h-1][w-1] == "#" or s[h-1][w-1] == 10**4:
    print(-1)
else:
    print(white-1 - s[h-1][w-1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[45]:


H,W = map(int, input().split())
grid = ["#"*(W+2)]
b_cnt = 0
for _ in range(H):
    tmp = input()
    b_cnt += tmp.count("#")
    grid.append("#"+tmp+"#")
grid.append("#"*(W+2))


# In[46]:


dp = [[H*W+1]*(W+2) for _ in range(H+2)]
move = [
    [-1,0],[1,0],[0,-1],[0,1]
]

pos = [1,1]

dp[pos[0]][pos[1]] = 1

pos_list = [pos]


while len(pos_list)>0:
#     print("pos_list=%s"%pos_list)
    pos = pos_list.pop()
    cnt = dp[pos[0]][pos[1]]
    for mx,my in move:
        if my == 0 and mx == 0:
            continue
        n_pos = [pos[0]+mx, pos[1]+my]
        if grid[n_pos[0]][n_pos[1]] != "#":
            if dp[n_pos[0]][n_pos[1]] != H*W+1:
                dp[n_pos[0]][n_pos[1]] = min(dp[n_pos[0]][n_pos[1]], cnt+1)
            else:
                dp[n_pos[0]][n_pos[1]] = min(dp[n_pos[0]][n_pos[1]], cnt+1)
                pos_list.append(n_pos)
if dp[H][W] != H*W+1:
    ans = H*W - dp[H][W] - b_cnt
else:
    ans = -1
print(ans)


# In[ ]:

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W = map(int, input().split())
maze=[input() for _ in range(H)]
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
dis = [[-1 for _ in range(W)] for _ in range(H)]
dis[0][0] = 0
q = deque()
q.append((0,0))
while len(q) > 0:
    e = q.popleft()
    y, x = e[0], e[1]
    for i in range(4):
        if 0 <= y + dy[i] < H:
            if 0 <= x + dx[i] < W:
                if dis[y + dy[i]][x + dx[i]] != -1:
                    continue
                if maze[y + dy[i]][x + dx[i]] == '.':
                    dis[y + dy[i]][x + dx[i]] = dis[y][x] + 1
                    q.append((y + dy[i], x + dx[i]))
cross = 0
for i in range(H):
  for j in range(W):
    if maze[i][j] == "#":
      cross += 1
print(H * W -dis[H-1][W-1] - cross -1 )

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W = map(int, input().split())
maze=[input() for _ in range(H)]
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
dis = [[-1 for _ in range(W)] for _ in range(H)]
dis[0][0] = 0
q = deque()
q.append((0,0))
while len(q) > 0:
    e = q.popleft()
    y, x = e[0], e[1]
    for i in range(4):
        if 0 <= y + dy[i] < H:
            if 0 <= x + dx[i] < W:
                if dis[y + dy[i]][x + dx[i]] != -1:
                    continue
                if maze[y + dy[i]][x + dx[i]] == '.':
                    dis[y + dy[i]][x + dx[i]] = dis[y][x] + 1
                    q.append((y + dy[i], x + dx[i]))
cross = 0
for i in range(H):
  for j in range(W):
    if maze[i][j] == "#":
      cross += 1
print(dis)
print(H * W -dis[H-1][W-1] - cross -1 )

Submission status: Wrong Answer

Solution code:
from collections import deque

H,W=map(int,input().split())
S=[[x for x in input()] for _ in range(H)]

count=0
for i in range(len(S)):
  count+=S[i].count('.')

d=deque([(0,0)])
distance=[[-1]*W for _ in range(H)]
distance[0][0]=0
goal_distance=0

while d:
  h,w=d.popleft()
  if h==H-1 and w==W-1:
    goal_distance=distance[h][w]
    break
  
  if h-1>=0 and S[h-1][w]=='.':
    d.append((h-1,w))
    distance[h-1][w]=distance[h][w]+1
    S[h-1][w]='#'
    
  if h+1<=H-1 and S[h+1][w]=='.':
    d.append((h+1,w))
    distance[h+1][w]=distance[h][w]+1
    S[h+1][w]='#'
    
  if w-1>=0 and S[h][w-1]=='.':
    d.append((h,w-1))
    distance[h][w-1]=distance[h][w]+1
    S[h][w-1]='#'
    
  if w+1<=W-1 and S[h][w+1]=='.':
    d.append((h,w+1))
    distance[h][w+1]=distance[h][w]+1
    S[h][w+1]='#'

print(count-(goal_distance+1))

Submission status: Wrong Answer

Solution code:
from collections import deque

H,W=map(int,input().split())
S=[[x for x in input()] for _ in range(H)]

count=0
for i in range(len(S)):
  count+=S[i].count('.')

d=deque([(0,0)])
distance=[[-1]*W for _ in range(H)]
distance[0][0]=0

while d:
  h,w=d.popleft()
  if h==H-1 and w==W-1:
    break
  
  if h-1>=0 and S[h-1][w]=='.':
    d.append((h-1,w))
    distance[h-1][w]=distance[h][w]+1
    S[h-1][w]='#'
    
  if h+1<=H-1 and S[h+1][w]=='.':
    d.append((h+1,w))
    distance[h+1][w]=distance[h][w]+1
    S[h+1][w]='#'
    
  if w-1>=0 and S[h][w-1]=='.':
    d.append((h,w-1))
    distance[h][w-1]=distance[h][w]+1
    S[h][w-1]='#'
    
  if w+1<=W-1 and S[h][w+1]=='.':
    d.append((h,w+1))
    distance[h][w+1]=distance[h][w]+1
    S[h][w+1]='#'
    
goal_distance=max([max(d) for d in distance])
print(count-(goal_distance+1))

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]

s = 0
for c in S:
    s += c.count('.')

que = deque()
dist = [[-1 for j in range(W)] for i in range(H)]
dist[0][0] = 0
st = (0, 0)
que.append(st)
dydx = [(1, 0), (0, 1), (-1, 0), (0, -1)]

while que:
    p, q = que.popleft()

    for dy, dx in dydx:
        print(dy, dx)
        if 0 <= p+dy and p+dy < H and 0 <= q+dx and q+dx < W:
            if dist[p+dy][q+dx] != -1 or S[p+dy][q+dx] != '.':
                continue

            dist[p+dy][q+dx] = dist[p][q] + 1
            que.append((p+dy, q+dx))

if dist[H-1][W-1] == -1:
    print(-1)
else:
    print(s-dist[H-1][W-1]-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
queue=deque()
s=[list(input()) for _ in range(h)]
seen=[[False]*w for _ in range(h)]
queue.append([0,0])
ans=[[1000000]*w for _ in range(h)]
ans[0][0]=1
for i in range(h*w*100):
    if len(queue)==0:
        break
    cnt=queue.popleft()
    seen[cnt[0]][cnt[1]]=True
    if cnt[0]!=0:
        if seen[cnt[0]-1][cnt[1]]==False and s[cnt[0]-1][cnt[1]]==".":
            queue.append([cnt[0]-1,cnt[1]])
            ans[cnt[0]-1][cnt[1]]=min(ans[cnt[0]-1][cnt[1]],ans[cnt[0]][cnt[1]]+1)
    if cnt[1]!=0:
        if seen[cnt[0]][cnt[1]-1]==False and s[cnt[0]][cnt[1]-1]==".":
            queue.append([cnt[0],cnt[1]-1])
            ans[cnt[0]][cnt[1]-1]=min(ans[cnt[0]][cnt[1]-1],ans[cnt[0]][cnt[1]]+1)
    if cnt[0]!=h-1:
        if seen[cnt[0]+1][cnt[1]]==False and s[cnt[0]+1][cnt[1]]==".":
            queue.append([cnt[0]+1,cnt[1]])
            ans[cnt[0]+1][cnt[1]]=min(ans[cnt[0]+1][cnt[1]],ans[cnt[0]][cnt[1]]+1)
    if cnt[1]!=w-1:
        if seen[cnt[0]][cnt[1]+1]==False and s[cnt[0]][cnt[1]+1]==".":
            queue.append([cnt[0],cnt[1]+1])
            ans[cnt[0]][cnt[1]+1]=min(ans[cnt[0]][cnt[1]+1],ans[cnt[0]][cnt[1]]+1)
cnt2=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            cnt2+=1
if seen[-1][-1]==False:
    print(-1)
else:
    print(cnt2-ans[-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
queue=deque()
s=[list(input()) for _ in range(h)]
seen=[[False]*w for _ in range(h)]
queue.append([0,0])
ans=[[10000]*w for _ in range(h)]
ans[0][0]=1
for i in range(h*w*15):
    if len(queue)==0:
        break
    cnt=queue.popleft()
    seen[cnt[0]][cnt[1]]=True
    if cnt[0]!=0:
        if seen[cnt[0]-1][cnt[1]]==False and s[cnt[0]-1][cnt[1]]==".":
            queue.append([cnt[0]-1,cnt[1]])
            ans[cnt[0]-1][cnt[1]]=min(ans[cnt[0]-1][cnt[1]],ans[cnt[0]][cnt[1]]+1)
    if cnt[1]!=0:
        if seen[cnt[0]][cnt[1]-1]==False and s[cnt[0]][cnt[1]-1]==".":
            queue.append([cnt[0],cnt[1]-1])
            ans[cnt[0]][cnt[1]-1]=min(ans[cnt[0]][cnt[1]-1],ans[cnt[0]][cnt[1]]+1)
    if cnt[0]!=h-1:
        if seen[cnt[0]+1][cnt[1]]==False and s[cnt[0]+1][cnt[1]]==".":
            queue.append([cnt[0]+1,cnt[1]])
            ans[cnt[0]+1][cnt[1]]=min(ans[cnt[0]+1][cnt[1]],ans[cnt[0]][cnt[1]]+1)
    if cnt[1]!=w-1:
        if seen[cnt[0]][cnt[1]+1]==False and s[cnt[0]][cnt[1]+1]==".":
            queue.append([cnt[0],cnt[1]+1])
            ans[cnt[0]][cnt[1]+1]=min(ans[cnt[0]][cnt[1]+1],ans[cnt[0]][cnt[1]]+1)
cnt2=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            cnt2+=1
if seen[-1][-1]==False:
    print(-1)
else:
    print(cnt2-ans[-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
queue=deque()
s=[list(input()) for _ in range(h)]
seen=[[False]*w for _ in range(h)]
queue.append([0,0])
ans=[[0]*w for _ in range(h)]
ans[0][0]=1
for i in range(h*w*10):
    if len(queue)==0:
        break
    cnt=queue.popleft()
    seen[cnt[0]][cnt[1]]=True
    if cnt[0]!=0:
        if seen[cnt[0]-1][cnt[1]]==False and s[cnt[0]-1][cnt[1]]==".":
            queue.append([cnt[0]-1,cnt[1]])
            ans[cnt[0]-1][cnt[1]]=ans[cnt[0]][cnt[1]]+1
    if cnt[1]!=0:
        if seen[cnt[0]][cnt[1]-1]==False and s[cnt[0]][cnt[1]-1]==".":
            queue.append([cnt[0],cnt[1]-1])
            ans[cnt[0]][cnt[1]-1]=ans[cnt[0]][cnt[1]]+1
    if cnt[0]!=h-1:
        if seen[cnt[0]+1][cnt[1]]==False and s[cnt[0]+1][cnt[1]]==".":
            queue.append([cnt[0]+1,cnt[1]])
            ans[cnt[0]+1][cnt[1]]=ans[cnt[0]][cnt[1]]+1
    if cnt[1]!=w-1:
        if seen[cnt[0]][cnt[1]+1]==False and s[cnt[0]][cnt[1]+1]==".":
            queue.append([cnt[0],cnt[1]+1])
            ans[cnt[0]][cnt[1]+1]=ans[cnt[0]][cnt[1]]+1
cnt2=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            cnt2+=1
if seen[-1][-1]==False:
    print(-1)
else:
    print(cnt2-ans[-1][-1])

Submission status: Wrong Answer

Solution code:
t,*g=open(0);h,w=map(int,t.split());v=[[0]*w for _ in range(h)];v[0][0]=1;q=[(0,0)]
while q:
	a,b=q.pop(0)
	for c,d in((-1,0),(0,-1),(1,0),(0,1)):
		x=a+c;y=b+d
		if w>x>=0<=y<h and v[y][x]<1 and g[y][x]==".":q+=[(x,y)];v[y][x]=v[b][a]+1
t=v[-1][-1];print((-1,t-sum(c.count(".")for c in g))[t>0])

Submission status: Wrong Answer

Solution code:
from collections import deque


dh = [-1, 0, 1, 0]
dw = [0, 1, 0, -1]


H, W = map(int, input().split())
G = [input() for _ in range(H)]


def count_black():
    res = 0
    for i in range(H):
        for j in range(W):
            if G[i][j] == '#':
                res += 1
    return res


def minimum_step():
    dist = [[0] * W for _ in range(H)]
    dist[0][0] = 1
    que = deque([[0, 0]])

    while que:
        h, w = que.popleft()
        for i in range(4):
            nh = h + dh[i]
            nw = w + dw[i]
            if nh < 0 or nw < 0 or nh >= H or nw >= W:
                continue
            if G[nh][nw] == '#':
                continue
            if dist[nh][nw]:
                continue
            dist[nh][nw] = dist[h][w] + 1
            que.append([nh, nw])
    return dist[H - 1][W - 1]


if __name__ == "__main__":
    print(H * W - minimum_step() - count_black())

Submission status: Wrong Answer

Solution code:
import numpy as np


h, w, *s = open(0).read().split()
h = int(h)
w = int(w)
all = sum(t.count('#') for t in s)

inf = 1 << 30
d = np.full((h + 1, w + 1), inf, dtype=np.int)

d[0][0] = 0
q = [(0, 0)]
for v in q:
    for nv in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
        ny = v[0] + nv[0]
        nx = v[1] + nv[1]
        if 0 <= nx < w and 0 <= ny < h and d[ny][nx] == inf and s[ny][nx] == '.':
            d[ny][nx] = d[v[0]][v[1]] + 1
            q.append((ny, nx))

print(h * w - (d[h - 1][w - 1] + 1) - all)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
M = [str(input()) for _ in range(h)]

cnt = 0
for i in range(h):
    cnt += M[i].count('.')

M = ['#'*w] + M + ['#'*w]
M = ['#'+c+'#' for c in M]

sy, sx = 1, 1

from collections import deque
stack = deque()
stack.append((sy, sx))
visit = [[-1]*(w+2) for _ in range(h+2)]
visit[sy][sx] = 0

while stack:
    y, x = stack.pop()
    if y == h and x == w:
        break
    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
        new_y, new_x = y+dy, x+dx
        if visit[new_y][new_x] == -1 and M[new_y][new_x] != '#':
            visit[new_y][new_x] = visit[y][x]+1
            stack.append((new_y, new_x))
else:
    print(-1)
    exit()

print(cnt-(visit[h][w]+1))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
M = [str(input()) for _ in range(h)]

cnt = 0
for i in range(h):
    cnt += M[i].count('.')

M = ['#'*w] + M + ['#'*w]
M = ['#'+c+'#' for c in M]

sy, sx = 1, 1

from collections import deque
stack = deque()
stack.append((sy, sx))
visit = [[-1]*(w+2) for _ in range(h+2)]
visit[sy][sx] = 0

while stack:
    y, x = stack.pop()
    if y == h and x == w:
        break
    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
        new_y, new_x = y+dy, x+dx
        if visit[new_y][new_x] == -1 and M[new_y][new_x] != '#':
            visit[new_y][new_x] = visit[y][x]+1
            stack.append((new_y, new_x))
else:
    print(-1)

print(cnt-(visit[h][w]+1))

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int,input().split())
s = []
black = 0
for _ in range(H):
    t = input()
    for c in t:
        if c == "#":
            black += 1
    s.append(t)
q = deque([(0,0,1)])
while q:
    x,y,d = q.popleft()
    if x == H-1 and y == W-1:
        print(H*W-d-black)
        exit(0)
    for nx,ny in [(x+1,y), (x,y+1)]:
        if nx < H and ny < W and s[nx][ny] == ".":
            q.append((nx,ny,d+1))
print(-1)

Submission status: Wrong Answer

Solution code:
import queue
h, w = map(int, input().split())
s = []
black_num = 0
for i in range(h):
    s_ij = input()
    s.append(s_ij)
    for j in range(w):
        if s_ij[j] == "#":
            black_num += 1

already_pass = set()
already_pass.add((1, 1))
q = queue.Queue()
q.put([(1, 1), 1])

while not q.empty():
    x_y, l = q.get()
    x = x_y[0]
    y = x_y[1]
    if x == h and y == w:
        print(h * w - black_num - l)
        break
    next_xy = [(max(0, x - 1), y), (min(h, x + 1), y),
               (x, max(0, y - 1)), (x, min(w, y + 1))]
    for i in next_xy:
        if i not in already_pass and s[i[0]-1][i[1]-1] != "#":
            q.put([i, l + 1])
            already_pass.add(i)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import queue
h, w = map(int, input().split())
s = []
black_num = 0
for i in range(h):
    s_ij = input()
    s.append(s_ij)
    for j in range(w):
        if s_ij[j] == "#":
            black_num += 1

already_pass = set()
q = queue.Queue()
q.put([(1, 1), 1])

while not q.empty():
    x_y, l = q.get()
    x = x_y[0]
    y = x_y[1]
    if x == h and y == w:
        print(h * w - black_num - l)
        break
    next_xy = [(max(0, x - 1), y), (min(h, x + 1), y),
               (x, max(0, y - 1)), (x, min(w, y + 1))]
    for i in next_xy:
        if i not in already_pass and s[i[0]-1][i[1]-1] != "#":
            q.put([i, l + 1])
            already_pass.add(i)

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = list(map(int, input().split()))

s = [input() for _ in range(h)]

c = 0

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            c += 1

#DFS
q = deque([[0, 0]])
dist = [[10000] * w for _ in range(h)]
dist[0][0] = 0

flag = False

while len(q) > 0:
    i, j = q.pop()
    if i == h-1 and j == w-1:
        break
    else:
        if i < h-1 and s[i+1][j] == "." and not dist[i+1][j] < 10000:
            q.append([i+1, j])
            dist[i+1][j] = dist[i][j] + 1
        if j < w-1 and s[i][j+1] == "." and not dist[i][j+1] < 10000:
            q.append([i, j+1])
            dist[i][j+1] = dist[i][j] + 1
        if i > 0 and s[i-1][j] == "." and not dist[i-1][j] < 10000:
            q.append([i-1, j])
            dist[i-1][j] = dist[i][j] + 1
        if j > 0 and s[i][j-1] == "." and not dist[i][j-1] < 10000:
            q.append([i, j-1])
            dist[i][j-1] = dist[i][j] + 1

if dist[h-1][w-1] < 10000:
    print(h * w - dist[h-1][w-1] - 1 - c)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = list(map(int, input().split()))

s = [input() for _ in range(h)]

c = 0

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            c += 1

#DFS
q = deque([[0, 0]])
visited = [[False] * w for _ in range(h)]
visited[0][0] = True

flag = False

while len(q) > 0:
    i, j = q.popleft()
    if i == h-1 and j == w-1:
        flag = True
        break
    else:
        if i < h-1 and s[i+1][j] == "." and not visited[i+1][j]:
            q.append([i+1, j])
            visited[i+1][j] = True
        if j < w-1 and s[i][j+1] == "." and not visited[i][j+1]:
            q.append([i, j+1])
            visited[i][j+1] = True

if flag:
    print((h-1)*(w-1)-c)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[list(input())for _ in range(H)]

from collections import deque
def bfs(h,w,sy,sx,gy,gx,S):
    maze=[[10**9]*(W)for _ in range(H)]
    maze[sy-1][sx-1]=0
    que=deque([[sy-1,sx-1]])
    while que:
        y,x=que.popleft()
        for i,j in [(1,0),(0,1),(-1,0),(0,-1)]:
            nexty,nextx=y+i,x+j
            if 0<=nexty<h and 0<=nextx<w:
                dist1=S[nexty][nextx]
                dist2=maze[nexty][nextx]
            else:
                continue
            if dist1!='#':
                if dist2>maze[y][x]+1:
                    maze[nexty][nextx]=maze[y][x]+1
                    que.append([nexty,nextx])
    return maze[gy-1][gx-1]+1

A=0
for s in S:
    A+=s.count('.')

B=bfs(H,W,1,1,H,W,S)
if B==10**9:
    ans=-1
else:
    ans=A-B
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

q = deque()
q.append((0, 0, 0))
check = [[0]*w for _ in range(h)]

while q:
    y, x, d = q.popleft()
    if y == h-1 and x == w-1:
        g = d
        break
    if check[y][x] == 0:
        check[y][x] = 1
        for xdx, ydy in zip((x, x, x+1, x-1), (y+1, y-1, y, y)):
            if (0 <= xdx < w) and (0 <= ydy < h):
                if check[ydy][xdx] == 0 and s[ydy][xdx] == '.':
                    q.append((ydy, xdx, d+1))
ss = 0
for i in s:
    ss += i.count('#')
print(h*w-ss-d-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

def get_next(x, y, costs, S, cost):
    ans = list()
    if x != 0 and costs[x-1][y] == -1 and S[x-1][y] == ".":
        ans.append([x-1, y])
        costs[x-1][y] = cost + 1

    if x != H-1 and costs[x+1][y] == -1 and S[x+1][y] == ".":
        ans.append([x+1, y])
        costs[x+1][y] = cost + 1

    if y != 0 and costs[x][y-1] == -1 and S[x][y-1] == ".":
        ans.append([x, y-1])
        costs[x][y-1] = cost + 1

    if y != W-1 and costs[x][y+1] == -1 and S[x-1][y] == ".":
        ans.append([x, y+1])
        costs[x][y+1] = cost + 1

    return ans

costs = [[-1 for _ in range(W)] for _ in range(H)]
costs[0][0] = 0
q = deque()
q.append([0, 0])
while len(q) and costs[H-1][W-1] == -1:
    now = q.popleft()
    nxt = get_next(now[0], now[1], costs, S, costs[now[0]][now[1]])
    for dot in nxt:
        q.append([dot[0], dot[1]])
    
fin_cost = costs[H-1][W-1]
if fin_cost == -1:
    print(-1)
else:
    w_cnt = 0
    for i in range(H):
        for j in range(W):
            if S[i][j] == ".":
                w_cnt += 1
    print(w_cnt - fin_cost - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
S = [list(input().split()) for _ in range(H)]

def get_next(x, y, costs, S, cost):
    ans = list()
    if x != 0 and costs[x-1][y] == -1 and S[x-1][0][y] == ".":
        ans.append([x-1, y])
        costs[x-1][y] = cost + 1

    if x != H-1 and costs[x+1][y] == -1 and S[x+1][0][y] == ".":
        ans.append([x+1, y])
        costs[x+1][y] = cost + 1

    if y != 0 and costs[x][y-1] == -1 and S[x][0][y-1] == ".":
        ans.append([x, y-1])
        costs[x][y-1] = cost + 1

    if y != W-1 and costs[x][y+1] == -1 and S[x-1][0][y] == ".":
        ans.append([x, y+1])
        costs[x][y+1] = cost + 1

    return ans

costs = [[-1 for _ in range(W)] for _ in range(H)]
costs[0][0] = 0
q = deque()
q.append([0, 0])
while len(q):
    now = q.popleft()
    nxt = get_next(now[0], now[1], costs, S, costs[now[0]][now[1]])
    for dot in nxt:
        q.append([dot[0], dot[1]])
    
fin_cost = costs[H-1][W-1]
if fin_cost == -1:
    print(-1)
else:
    b_cnt = 0
    for i in range(H):
        for j in range(W):
            if S[i][0][j] == "#":
                b_cnt += 1
    print(H*W - b_cnt - fin_cost - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
S = [list(input().split()) for _ in range(H)]

def get_next(x, y, costs, S, cost):
    ans = list()
    if x != 0 and costs[x-1][y] == -1 and S[x-1][0][y] == ".":
        ans.append([x-1, y])
        costs[x-1][y] = cost + 1

    if x != H-1 and costs[x+1][y] == -1 and S[x+1][0][y] == ".":
        ans.append([x+1, y])
        costs[x+1][y] = cost + 1

    if y != 0 and costs[x][y-1] == -1 and S[x][0][y-1] == ".":
        ans.append([x, y-1])
        costs[x][y-1] = cost + 1

    if y != W-1 and costs[x][y+1] == -1 and S[x-1][0][y] == ".":
        ans.append([x, y+1])
        costs[x][y+1] = cost + 1

    return ans

costs = [[-1 for _ in range(W)] for _ in range(H)]
costs[0][0] = 0
q = deque()
q.append([0, 0])
while len(q):
    now = q.popleft()
    nxt = get_next(now[0], now[1], costs, S, costs[now[0]][now[1]])
    for dot in nxt:
        q.append([dot[0], dot[1]])
    
fin_cost = costs[H-1][W-1]
b_cnt = 0
for i in range(H):
    for j in range(W):
        if S[i][0][j] == "#":
            b_cnt += 1
print(H*W - b_cnt - fin_cost - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
C=list(list(input()) for _ in range(H))

def bfs():
	sx, sy= 0, 0
	gx, gy= H-1, W-1
	dx = [1, 0, -1, 0]
	dy = [0, 1, 0, -1]

	work_queue=deque([])
	work_queue.append((sx, sy))
	visited=[[float("inf")]*W for _ in range(H)]
	visited[sx][sy]=1

	while work_queue:
		x, y = work_queue.popleft()

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

			if 0<=nx<H and 0<=ny<W and visited[nx][ny]==float("inf") and C[x][ny]!="#":
				work_queue.append((nx, ny))
				visited[nx][ny]= visited[x][y]+1
	return visited[gx][gy]


temp=bfs()

black=0
white=0
for i in range(H):
	for j in range(W):
		if C[i][j]==".":
			white+=1
		else:
			black+=1

if temp==float("inf"):
	print(-1)
else:
	print(white-temp)