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:
import math,itertools,fractions,heapq,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  q=[]

  field=[S() for _ in range(h)]
  counter=[[inf]*w for _ in range(h)]

  visit_counter=[[0]*w for _ in range(h)]

  sharp_cnt=0
  for x in field:
    sharp_cnt+=x.count('#')

  q.append((0,0,0))

  while len(q)>0:
    y,x,cnt=q.pop()
    counter[y][x]=cnt
    visit_counter[y][x]+=1

    for dy,dx in dd:
      ny=y+dy
      nx=x+dx

      if 0<=ny and 0<=nx and ny<h and nx<w:
        if field[ny][nx]!='#':

          if counter[ny][nx]>cnt:
            if visit_counter[ny][nx]<50:
              q.append((ny,nx,cnt+1))

  step_cnt=counter[h-1][w-1]+1

  if step_cnt==inf+1:
    return -1

  return h*w-sharp_cnt-step_cnt

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  q=[]

  field=[S() for _ in range(h)]
  counter=[[inf]*w for _ in range(h)]

  visit_counter=[[0]*w for _ in range(h)]

  sharp_cnt=0
  for x in field:
    sharp_cnt+=x.count('#')

  q.append((0,0,0))

  while len(q)>0:
    y,x,cnt=q.pop()
    counter[y][x]=cnt
    visit_counter[y][x]+=1

    for dy,dx in dd:
      ny=y+dy
      nx=x+dx

      if 0<=ny and 0<=nx and ny<h and nx<w:
        if field[ny][nx]!='#':

          if counter[ny][nx]>cnt:
            if visit_counter[ny][nx]<5:
              q.append((ny,nx,cnt+1))

  step_cnt=counter[h-1][w-1]+1

  if step_cnt==inf+1:
    return -1

  return h*w-sharp_cnt-step_cnt

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  q=[]

  field=[S() for _ in range(h)]
  counter=[[inf]*w for _ in range(h)]

  sharp_cnt=0
  for x in field:
    sharp_cnt+=x.count('#')

  q.append((0,0,0))

  while len(q)>0:
    y,x,cnt=q.pop()
    counter[y][x]=cnt

    for dy,dx in dd:
      ny=y+dy
      nx=x+dx

      if 0<=ny and 0<=nx and ny<h and nx<w:
        if field[ny][nx]!='#':
          if counter[ny][nx]>cnt:
            q.append((ny,nx,cnt+1))

  return h*w-sharp_cnt-counter[h-1][w-1]-1

# main()
print(main())

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
#0が進めるところ、1は無理
#lis = [[1 if l == '#' else 0 for l in list(input())] for _ in range(h)]

road_count = 0
lis = [list('#'*(w+1))]
for _ in range(h):
    li = input()
    for i in li:
        if i == '.':
            road_count += 1
    li += '#'
    lis.append(list(li))
lis.append(list('#'*(w+1)))

#print(lis)
dp = [[0]*(w+1) for _ in range(h+1)]

#(開始x,開始y,移動回数)
step = [[0,0]]
while len(step)>0:
    x,y = step.pop(0)

    if x == h-1 and y == w-1:
        print(road_count-dp[x][y]-1)
        exit()

    #探索済みのところは、#にする
    lis[x][y] = '#'


    if lis[x-1][y] != '#' and dp[x-1][y] == 0:
        step.append([x-1,y])
        dp[x-1][y] = dp[x][y]+1
    if lis[x+1][y] != '#' and dp[x+1][y] == 0:
        step.append([x+1,y])
        dp[x+1][y] = dp[x][y]+1
    if lis[x][y-1] != '#' and dp[x][y-1] == 0:
        step.append([x,y-1])
        dp[x][y-1] = dp[x][y]+1
    if lis[x][y+1] != '#' and dp[x][y+1] == 0:
        step.append([x,y+1])
        dp[x][y+1] = dp[x][y]+1
print(-1)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
#0が進めるところ、1は無理
#lis = [[1 if l == '#' else 0 for l in list(input())] for _ in range(h)]

road_count = 0
lis = [list('#'*(w+1))]
for _ in range(h):
    li = input()
    for i in li:
        if i == '.':
            road_count += 1
    li += '#'
    lis.append(list(li))
lis.append(list('#'*(w+1)))

#print(lis)
dp = [[0]*(w+1) for _ in range(h+1)]

#(開始x,開始y,移動回数)
step = [[0,0]]
while len(step)>0:
    x,y = step.pop(0)

    if x == h-1 and y == w-1:
        print(road_count-dp[x][y]-1)
        exit()

    #探索済みのところは、#にする
    lis[x][y] = '#'


    if lis[x-1][y] != '#':
        step.append([x-1,y])
        dp[x-1][y] = dp[x][y]+1
    if lis[x+1][y] != '#':
        step.append([x+1,y])
        dp[x+1][y] = dp[x][y]+1
    if lis[x][y-1] != '#':
        step.append([x,y-1])
        dp[x][y-1] = dp[x][y]+1
    if lis[x][y+1] != '#':
        step.append([x,y+1])
        dp[x][y+1] = dp[x][y]+1
print(-1)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
#0が進めるところ、1は無理
#lis = [[1 if l == '#' else 0 for l in list(input())] for _ in range(h)]

road_count = 0
lis = [list('#'*(w+2))]
for _ in range(h):
    li = input()
    for i in li:
        if i == '.':
            road_count += 1
    li = '#' + li + '#'
    lis.append(list(li))
lis.append(list('#'*(w+2)))

#print(lis)
dp = [[0]*(w+2) for _ in range(h+2)]

#(開始x,開始y,移動回数)
step = [[1,1]]
while len(step)>0:
    x,y = step.pop(0)

    if x == h and y == w:
        count = dp[x][y]
        break

    #探索済みのところは、#にする
    lis[x][y] = '#'


    if lis[x-1][y] != '#':
        step.append([x-1,y])
        dp[x-1][y] = dp[x][y]+1
    if lis[x+1][y] != '#':
        step.append([x+1,y])
        dp[x+1][y] = dp[x][y]+1
    if lis[x][y-1] != '#':
        step.append([x,y-1])
        dp[x][y-1] = dp[x][y]+1
    if lis[x][y+1] != '#':
        step.append([x,y+1])
        dp[x][y+1] = dp[x][y]+1

if dp[x][y] == 0:
    print(-1)
else:
    #print(dp)
    #print(road_count,dp[x][y])
    print(road_count - dp[x][y] - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int,input().split())
#0が進めるところ、1は無理
#lis = [[1 if l == '#' else 0 for l in list(input())] for _ in range(h)]

road_count = 0
lis = [list('#'*(w+1))]
for _ in range(h):
    li = input()
    for i in li:
        if i == '.':
            road_count += 1
    li += '#' + li + '#'
    lis.append(list(li))
lis.append(list('#'*(w+1)))

dp = [[0]*(w+2) for _ in range(h+2)]

count = 0
#(開始x,開始y,移動回数)
step = [[1,1]]
while len(step)>0:
    x,y = step.pop(0)

    if x == h and y == w:
        count = dp[x][y]
        break

    #探索済みのところは、#にする
    lis[x][y] = '#'


    if lis[x-1][y] != '#':
        step.append([x-1,y])
        dp[x-1][y] = dp[x][y]+1
    if lis[x+1][y] != '#':
        step.append([x+1,y])
        dp[x+1][y] = dp[x][y]+1
    if lis[x][y-1] != '#':
        step.append([x,y-1])
        dp[x][y] = dp[x][y]+1
    if lis[x][y-1] != '#':
        step.append([x,y+1])
        dp[x][y+1] = dp[x][y]+1

if dp[x][y] == 0:
    print(-1)
else:
    print(road_count - dp[x][y] - 1)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
#0が進めるところ、1は無理
lis = [[1 if l == '#' else 0 for l in list(input())] for _ in range(h)]
#ゴールは9
lis[h-1][w-1] = 9
#print(lis)
#[[0, 0, 1], [1, 0, 0], [0, 0, 9]]

#この問題ではbfsを始める前に、もともと壁だったところの数を記録しておく
road = 0
for row in lis:
    for column in row:
        if column != 1:
            road +=1

count = 0
#(開始x,開始y,移動回数)
pos = [[0,0,0]]
while len(pos)>0:
    x,y,depth = pos.pop(0)

    if lis[x][y] == 9:
        count = depth
        break

    #探索済みのところは、1にする
    lis[x][y] = 1

    #遷移先は0or9なので、1以外なら行ける
    if x-1>=0 and lis[x-1][y] != 1:
        pos.append([x-1,y,depth+1])
    if x+1<h and lis[x+1][y] != 1:
        pos.append([x+1,y,depth+1])
    if y-1>=0 and lis[x][y-1] != 1:
        pos.append([x,y-1,depth+1])
    if y+1<w and lis[x][y+1] != 1:
        pos.append([x,y+1,depth+1])

#(全マス) - (壁) - (移動する道順上のマスの数 ＝ 最短の移動回数＋１）
print(road-(count+1))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
#0が進めるところ、1は無理
lis = [[1 if l == '#' else 0 for l in list(input())] for _ in range(h)]
#ゴールは9
lis[h-1][w-1] = 9
#print(lis)
#[[0, 0, 1], [1, 0, 0], [0, 0, 9]]

#この問題ではbfsを始める前に、もともと壁だったところの数を記録しておく
wall = 0
for row in lis:
    for column in row:
        if column == 1:
            wall +=1

count = 0
#(開始x,開始y,移動回数)
pos = [[0,0,0]]
while len(pos)>0:
    x,y,depth = pos.pop(0)

    if lis[x][y] == 9:
        count = depth
        break

    #探索済みのところは、1にする
    lis[x][y] = 1

    #遷移先は0or9なので、1以外なら行ける
    if x-1>=0 and lis[x-1][y] != 1:
        pos.append([x-1,y,depth+1])
    if x+1<h and lis[x+1][y] != 1:
        pos.append([x+1,y,depth+1])
    if y-1>=0 and lis[x][y-1] != 1:
        pos.append([x,y-1,depth+1])
    if y+1<w and lis[x][y+1] != 1:
        pos.append([x,y+1,depth+1])

#(全マス) - (壁) - (移動する道順上のマスの数 ＝ 最短の移動回数＋１）
print(h*w-wall-(count+1))

Submission status: Wrong Answer

Solution code:
import sys
import queue

h, w = list(map(int, input().split()))
l = [input() for _ in range(h)]
checked = [[False for _ in range(w)] for __ in range(h)]

q = queue.Queue()
q.put((0, 0, 1))
checked[0][0] = True
while not q.empty():
    x, y, count = q.get()
    if checked[y][x]:
        continue
    checked[y][x] = True
    if x == w-1 and y == h-1:
        ans = 0
        for i in range(h):
            for j in range(w):
                if l[i][j] == ".":
                    ans += 1
        print(ans-count)
        sys.exit(0)
    if x > 0 and l[y][x-1] == "." and not checked[y][x-1]:
        q.put((x-1, y, count+1))
    if y > 0 and l[y-1][x] == "." and not checked[y-1][x]:
        q.put((x, y-1, count+1))
    if x < w-1 and l[y][x+1] == "." and not checked[y][x+1]:
        q.put((x+1, y, count+1))
    if y < h-1 and l[y+1][x] == "." and not checked[y+1][x]:
        q.put((x, y+1, count+1))
print(-1)

Submission status: Wrong Answer

Solution code:
h,w = (int(x) for x in input().split())
ab = []
count = 0
for _ in range(h):
    a = input()
    a = a+'#'
    ab.append(list(a))
    for i in a:
        if i == '.':
            count += 1
ab.append(list('#'*w))
dp = [[0 for i in range(w)] for j in range(h)]

step = [[0, 0]]
dp[0][0] = 1
res = 1000000000000000

while len(step) > 0:
    x, y = step.pop(0)
    if x == w-1 and y == h-1:
        res = min([count-dp[y][x], res])
    if ab[y+1][x] == '.' and dp[y+1][x] == 0 and y+1 < h and -1 < y+1:
        step.append([x, y+1])
        dp[y+1][x] += dp[y][x] + 1
    if ab[y][x+1] == '.' and dp[y][x+1] == 0 and x+1 < w and -1 < x+1:
        step.append([x+1, y])
        dp[y][x+1] += dp[y][x] + 1
    if ab[y-1][x] == '.' and dp[y-1][x] == 0 and y-1 < h and -1 < y-1:
        step.append([x-1, y])
        dp[y-1][x] += dp[y][x] + 1
    if ab[y][x-1] == '.' and dp[y][x-1] == 0 and x-1 < w and -1 < x-1:
        step.append([x, y-1])
        dp[y][x-1] += dp[y][x] + 1

if res != 1000000000000000:
    print(res)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
h,w = (int(x) for x in input().split())
ab = []
count = 0
for _ in range(h):
    a = input()
    a = a+'#'
    ab.append(list(a))
    for i in a:
        if i == '.':
            count += 1
ab.append(list('#'*w))
dp = [[0 for i in range(w)] for j in range(h)]

step = [[0, 0]]
dp[0][0] = 1

while len(step) > 0:
    x, y = step.pop(0)
    if x == w-1 and y == h-1:
        print(count-dp[y][x])
        exit()
    if ab[y+1][x] == '.' and dp[y+1][x] == 0 and y+1 < h and -1 < y+1:
        step.append([x, y+1])
        dp[y+1][x] += dp[y][x] + 1
    if ab[y][x+1] == '.' and dp[y][x+1] == 0 and x+1 < w and -1 < x+1:
        step.append([x+1, y])
        dp[y][x+1] += dp[y][x] + 1
    if ab[y-1][x] == '.' and dp[y-1][x] == 0 and y-1 < h and -1 < y-1:
        step.append([x-1, y])
        dp[y-1][x] += dp[y][x] + 1
    if ab[y][x-1] == '.' and dp[y][x-1] == 0 and x-1 < w and -1 < x-1:
        step.append([x, y-1])
        dp[y][x-1] += dp[y][x] + 1

print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque


def bfs(sx, sy, gx, gy):
    que = deque()
    que.append([sx, sy])
    d[sx][sy] = 0
    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (0 <= nx and nx < H and 0 <= ny and ny < W and s[nx][ny] != "#" and d[nx][ny] == -1):
                que.append([nx, ny])
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[gx][gy]


H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
d = [[-1] * W for _ in range(H)]
b = 0

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

for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            b += 1
l = bfs(0, 0, H-1, W-1)
print(H*W - b-l-1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
H,W = map(int,input().split())
move_x = [1,-1,0,0]
move_y = [0,0,1,-1]
Field = [[c for c in input()] for _ in range(H)]
seen = [[False for _ in range(W)] for _ in range(H)]
queue = []
black = 0
for i in range(H):
    for j in range(W):
        if Field[i][j] == '#':
            black += 1
dist = [[0 for _ in range(W)] for _ in range(H)]
res = -1 
def bfs(start_x,start_y,Field,seen,queue,dist):
    global res
    seen[start_x][start_y] = True
    for i in range(0,4):
        x = start_x + move_x[i]
        y = start_y + move_y[i]
        if x < 0 or y < 0 or x >= H or y >= W:
            continue #out of range
        if Field[x][y] == '#':
            continue #we don't visit black square.
        if seen[x][y]:
            continue #already visited
        if Field[x][y] in queue:
            continue #already in queue.
        dist[x][y] = dist[start_x][start_y] + 1
        if x == (H-1) and y == (W-1):
            res = H*W - dist[x][y] - black #H*W - counting(seen) - black 
        queue.append((x,y))
    for element in queue:
        x,y = element
        if seen[x][y]:
            continue
        seen[x][y] = True
        queue.remove(element)
        bfs(x,y,Field,seen,queue,dist)    
bfs(0,0,Field,seen,queue,dist)
print(res-1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
H,W = map(int,input().split())
move_x = [1,-1,0,0]
move_y = [0,0,1,-1]
Field = [[c for c in input()] for _ in range(H)]
seen = [[False for _ in range(W)] for _ in range(H)]
queue = []
black = 0
for i in range(H):
    for j in range(W):
        if Field[i][j] == '#':
            black += 1
dist = [[0 for _ in range(W)] for _ in range(H)]
res = -1 
def bfs(start_x,start_y,Field,seen,queue,dist):
    global res
    seen[start_x][start_y] = True
    for i in range(0,4):
        x = start_x + move_x[i]
        y = start_y + move_y[i]
        if x < 0 or y < 0 or x >= H or y >= W:
            continue #out of range
        if Field[x][y] == '#':
            continue #we don't visit black square.
        if seen[x][y]:
            continue #already visited
        if Field[x][y] in queue:
            continue #already in queue.
        if x == (H-1) and y == (W-1):
            dist[x][y] = dist[start_x][start_y] + 1
            res = H*W - dist[x][y] - black #H*W - counting(seen) - black 
        queue.append((x,y))
        dist[x][y] = dist[start_x][start_y] + 1
    for element in queue:
        x,y = element
        if seen[x][y]:
            continue
        seen[x][y] = True
        queue.remove(element)
        bfs(x,y,Field,seen,queue,dist)    
bfs(0,0,Field,seen,queue,dist)
print(res-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
li = [["" for i in range(w)]for j in range(h)]
point = 0
for i in range(h):
    s = stdin.readline().rstrip()
    point += s.count(".")
    for j in range(w):
        li[i][j] = s[j]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
que = deque()
lin = [[inf for i in range(w)]for j in range(h)]
lin[0][0] = 0
que.append([0,0])
while que:
    k = que.popleft()
    x = k[0]
    y = k[1]
    for i,j in zip(dx,dy):
        nx = x + i
        ny = y + j
        if nx < 0 or nx >= h or ny < 0 or ny >= w:
            continue
        elif li[nx][ny] == "#" :
            continue
        elif lin[nx][ny] == inf:
            que.append([nx,ny])
            lin[nx][ny] = lin[x][y]+1
kotae = lin[h-1][w-1]
print(point-kotae-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
point = 0

li = [["#" for i in range(w)]for j in range(h)]
for i in range(h):
    s = stdin.readline().rstrip()
    point += s.count(".")
    for j in range(w):
        li[i][j] = s[j]
def all_pairs_shortest_paths(W):
    # 頂点の数
    n = len(W)
    # 結果を格納する行列を用意する
    res = [[0] * n for i in range(n)]
    # 用意した行列を初期化する
    for i in range(n):
        for j in range(i, n):
            if i == j:
                val = 0
            elif W[i][j]:
                val = W[i][j]
            else:
                val = inf
            res[i][j] = res[j][i] = val
    # 動的計画法ですべての超点間の最短距離を求める
    for k in range(n):
        for u in range(n):
            for v in range(n):
                res[u][v] = min(res[u][v], res[u][k] + res[k][v])
    return res
def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

a = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        a[i][j] = i*w + j

lin = [[0 for i in range(h*w)]for j in range(h*w)]

for i in range(h):
    for j in range(w-1):
        if li[i][j] == "." and li[i][j+1] == ".":
            lin[a[i][j]][a[i][j+1]] = 1
            lin[a[i][j+1]][a[i][j]] = 1

for i in range(h-1):
    for j in range(w):
        if li[i][j] == "." and li[i+1][j] == ".":
            lin[a[i][j]][a[i+1][j]] = 1
            lin[a[i][j]][a[i+1][j]] = 1

lis = all_pairs_shortest_paths(lin)
print(point-lis[0][-1]-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
point = 0

li = [["#" for i in range(w)]for j in range(h)]
for i in range(h):
    s = stdin.readline().rstrip()
    point += s.count(".")
    for j in range(w):
        li[i][j] = s[j]

def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

a = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        a[i][j] = i*w + j

lin = [[0 for i in range(h*w)]for j in range(h*w)]

for i in range(h):
    for j in range(w-1):
        if li[i][j] == "." and li[i][j+1] == ".":
            lin[a[i][j]][a[i][j+1]] = 1
            lin[a[i][j+1]][a[i][j]] = 1

for i in range(h-1):
    for j in range(w):
        if li[i][j] == "." and li[i+1][j] == ".":
            lin[a[i][j]][a[i+1][j]] = 1
            lin[a[i][j]][a[i+1][j]] = 1

lis = dijkstra(0,lin)
if lis[h*w-1] == inf:
    print(-1)
    exit()

print(point-lis[-1]-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
point = 0

li = [["#" for i in range(w)]for j in range(h)]
for i in range(h):
    s = stdin.readline().rstrip()
    for j in range(w):
        if s[j] == ".":
            point += 1
        li[i][j] = s[j]

def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

a = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        a[i][j] = i*w + j

lin = [[0 for i in range(h*w)]for j in range(h*w)]

for i in range(h):
    for j in range(w-1):
        if li[i][j] == "." and li[i][j+1] == ".":
            lin[a[i][j]][a[i][j+1]] = 1
            lin[a[i][j+1]][a[i][j]] = 1

for i in range(h-1):
    for j in range(w):
        if li[i][j] == "." and li[i+1][j] == ".":
            lin[a[i][j]][a[i+1][j]] = 1
            lin[a[i][j]][a[i+1][j]] = 1

lis = dijkstra(0,lin)
print(lis)
if lis[h*w-1] == inf:
    print(-1)
    exit()

print(point-lis[-1])

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
point = 0

"""
li = [["#" for i in range(w)]for j in range(h)]
for i in range(h):
    s = stdin.readline().rstrip()
    for j in range(w):
        if s[j] == ".":
            point += 1
        li[i][j] = s[j]

def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

a = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        a[i][j] = i*w + j

lin = [[0 for i in range(h*w)]for j in range(h*w)]

for i in range(h):
    for j in range(w-1):
        if li[i][j] == "." and li[i][j+1] == ".":
            lin[a[i][j]][a[i][j+1]] = 1
            lin[a[i][j+1]][a[i][j]] = 1

for i in range(h-1):
    for j in range(w):
        if li[i][j] == "." and li[i+1][j] == ".":
            lin[a[i][j]][a[i+1][j]] = 1
            lin[a[i][j]][a[i+1][j]] = 1

lis = dijkstra(0,lin)
print(lis)
if lis[h*w-1] == inf:
    print(-1)
    exit()

print(point-lis[-1]-1)

"""

print(-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
point = 0
li = [["#" for i in range(w)]for j in range(h)]
for i in range(h):
    s = stdin.readline().rstrip()
    for j in range(w):
        if s[j] == ".":
            point += 1
        li[i][j] = s[j]

def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

a = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        a[i][j] = i*w + j

lin = [[0 for i in range(h*w)]for j in range(h*w)]

for i in range(h):
    for j in range(w-1):
        if li[i][j] == "." and li[i][j+1] == ".":
            lin[a[i][j]][a[i][j+1]] = 1
            lin[a[i][j+1]][a[i][j]] = 1

for i in range(h-1):
    for j in range(w):
        if li[i][j] == "." and li[i+1][j] == ".":
            lin[a[i][j]][a[i+1][j]] = 1
            lin[a[i][j]][a[i+1][j]] = 1

lis = dijkstra(0,lin)
if lis[h*w-1] == inf:
    print(-1)
    exit()

print(point-lis[-1]-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
point = 0
li = [["#" for i in range(w)]for j in range(h)]
for i in range(h):
    s = stdin.readline().rstrip()
    for j in range(w):
        if s[j] == "#":
            point += 1
        li[i][j] = s[j]

def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

a = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        a[i][j] = i*w + j

lin = [[0 for i in range(h*w)]for j in range(h*w)]

for i in range(h):
    for j in range(w-1):
        if li[i][j] == "." and li[i][j+1] == ".":
            lin[a[i][j]][a[i][j+1]] = 1
            lin[a[i][j+1]][a[i][j]] = 1

for i in range(h-1):
    for j in range(w):
        if li[i][j] == "." and li[i+1][j] == ".":
            lin[a[i][j]][a[i+1][j]] = 1
            lin[a[i][j]][a[i+1][j]] = 1

lis = dijkstra(0,lin)
if lis[h*w-1] == inf:
    print(-1)
    exit()

print(h*w-lis[h*w-1]-point-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
inf = 10**10
h,w = [int(x) for x in stdin.readline().rstrip().split()]
point = 0
li = [["#" for i in range(w)]for j in range(h)]
for i in range(h):
    s = stdin.readline().rstrip()
    for j in range(w):
        if s[j] == "#":
            point += 1
        li[i][j] = s[j]

def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

a = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        a[i][j] = i*w + j

lin = [[0 for i in range(h*w)]for j in range(h*w)]

for i in range(h):
    for j in range(w-1):
        if li[i][j] == "." and li[i][j+1] == ".":
            lin[a[i][j]][a[i][j+1]] = 1
            lin[a[i][j+1]][a[i][j]] = 1

for i in range(h-1):
    for j in range(w):
        if li[i][j] == "." and li[i+1][j] == ".":
            lin[a[i][j]][a[i+1][j]] = 1
            lin[a[i][j]][a[i+1][j]] = 1

lis = dijkstra(0,lin)
if lis[-1] == inf:
    print(-1)
    exit()

print(h*w-lis[-1]-point-1)

Submission status: Wrong Answer

Solution code:
H,W = [int(i) for i in input().split()]
maze = ['0'] * H
for i in range(H):
    maze[i] = list(input())
dx = [1,0,-1,0]
dy = [0,1,0,-1]
import collections
from collections import deque
que = deque()
dic = [[-1] * (W) for _ in range(H)]
dic[0][0] = 0
que.append([0,0])
while (len(que) != 0):
    k = que.popleft()
    x = k[0]
    y = k[1]
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if nx < 0 or nx >= H or ny < 0 or ny >= W:
            continue
        if maze[nx][ny] == "#" :
            continue
        if dic[nx][ny] == -1:
            que.append([nx,ny])
            dic[nx][ny] = dic[x][y] +1
ml = dic[H-1][W-1]
count = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            count+=1
print(count - ml -1)