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
def bfs():
    d = [[float("inf")] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
 
    que = deque([])
    que.append((sy, sx))
    d[sy][sx] = 0
 
    while que:
        p = que.popleft()
        if p[0] == gy and p[1] == gx:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[gy][gx]
h, w = map(int, input().split())
sy, sx = 0, 0
gy, gx = h - 1, w - 1
s = [list(input()) for i in range(h)]
black = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            black += 1
print(h * w - bfs() - black - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

def bfs():
  m = [[float('inf')]*w for _ in range(h)]
  que = deque([])
  que.append([0,0])
  m[0][0] = 0
  
  while que:
    cur = que.popleft()
    #if cur[0] == h and cur[1] == w:
    #  break
    
    for i in range(4):
      nh = cur[0] + dh[i]
      nw = cur [1] + dw[i]
      
      if nh >= 0 and nh < h and nw >= 0 and nw < w and \
          m[nh][nw] == float('inf') and par[nh][nw] == '.':
        que.append([nh,nw])
        m[nh][nw] = m[cur[0]][cur[1]] + 1
        
  return m

m = bfs()
cnt = 0
for i in range(h):
  for j in range(w):
    if par[i][j] == '.':
      cnt += 1

print(cnt - m[h-1][w-1] -1)

Submission status: Wrong Answer

Solution code:
from collections import deque
def gridbfs(grid,sy,sx):
    INF = 10**18

    gridy = len(grid)
    gridx = len(grid[0])

    dist = [[INF]*gridx for _ in range(gridy)]
    dist[sy][sx] = 0
    q = deque([(sy,sx)])

    while q:
        y,x = q.popleft()
        for dy,dx in [(-1,0),(1,0),(0,-1),(0,1)]:
            yy,xx = y+dy, x+dx
            if not (0 <= yy < gridy and 0 <= xx < gridx):
                continue
            if grid[yy][xx] == 'x':
                continue
            if dist[yy][xx] != INF:
                continue
            dist[yy][xx] = dist[y][x] + 1
            q.append((yy,xx))

    return dist
 
H,W = map(int,input().split())
grid = [input() for i in range(H)]
d = gridbfs(grid,0,0)
b = 0
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            b += 1
 
print(max(H*W-b-d[H-1][W-1]-1,-1))

Submission status: Wrong Answer

Solution code:

def resolve():
    H, W = map(int, input().split())
    grid = [["#"]*(W+2) for _ in range(H+2)]
    cnt = 0
    for i in range(1,H+1):
        g = list(input())
        cnt += g.count(".")
        grid[i][1:-1] = g

    q = [(1, 1)]
    dxy = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    visited = [[0] * (W + 2) for _ in range(H + 2)]
    visited[1][1] = 1
    ans = 0
    while q:
        x, y = q.pop()
        for dx, dy in dxy:
            nx = dx + x
            ny = dy + y
            if grid[ny][nx] != "#" and visited[ny][nx] == 0:
                visited[ny][nx] += 1 + visited[y][x]
                q.append((nx, ny))
    if visited[H][W] == 0:
        ans = -1
    else:
        ans = cnt - visited[H][W]

    return print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:

def resolve():
    H, W = map(int, input().split())
    grid = [["#"]*(W+2) for _ in range(H+2)]
    cnt = 0
    for i in range(1,H+1):
        g = list(input())
        cnt += g.count(".")
        grid[i][1:-1] = g

    q = [(1, 1)]
    dxy = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    visited = [[0] * (W + 2) for _ in range(H + 2)]
    visited[1][1] = 1
    ans = 0
    while q:
        x, y = q.pop()
        for dx, dy in dxy:
            nx = dx + x
            ny = dy + y
            if grid[ny][nx] != "#" and visited[ny][nx] == 0:
                visited[ny][nx] += 1 + visited[y][x]
                q.append((nx, ny))
    ans = cnt - visited[H][W]

    return print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:

def resolve():
    H, W = map(int, input().split())
    grid = [["#"]*(W+2) for _ in range(H+2)]
    cnt = 0
    for i in range(1,H+1):
        g = list(input())
        cnt += g.count("#")
        grid[i][1:-1] = g

    q = [(1, 1)]
    dxy = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    visited = [[0] * (W + 2) for _ in range(H + 2)]
    visited[1][1] = 1
    ans = 0
    while q:
        x, y = q.pop()
        for dx, dy in dxy:
            nx = dx + x
            ny = dy + y
            if grid[ny][nx] != "#" and visited[ny][nx] == 0:
                visited[ny][nx] += 1 + visited[y][x]
                q.append((nx, ny))
    step = visited[H][W]
    ans = (H*W)- (step + cnt)
    return print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s = [input() for h in range(H)]

# Number of white cells
white_cells = sum([s[h].count(".") for h in range(H)])

# h,w
record = [[[0,0]]]
li = []
if s[1][0] == ".":
    li.append([1,0])
if s[0][1] == ".":
    li.append([0,1])
record.append(li)

count = 1
while True:
    if record[count] == []:
        print(-1)
        break
    li = []
    for p in record[count]:
        h = p[0]
        w = p[1]
        li = []
        if h > 0 and s[h-1][w] == "." and [h-1, w] not in record[count-1]:
            li.append([h-1, w])
        if w > 0 and s[h][w-1] == "." and [h, w-1] not in record[count-1]:
            li.append([h, w-1])
        if h < H-1 and s[h+1][w] == "." and [h+1, w] not in record[count-1]:
            li.append([h+1, w])
        if w < W-1 and s[h][w+1] == "." and [h, w+1] not in record[count-1]:
            li.append([h, w+1])
    if [H-1, W-1] in li:
        print(white_cells - count - 2)
        break
    record.append(li)
    count += 1

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
cntW=0
for i in range(h):
  for j in range(w):
    if s[i][j]=='.':
      cntW+=1
dist=[[-1]*w for i in range(h)]
dist[0][0]=0
q=[(0,0)]
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while q:
  y,x=q.pop()
  for i in range(4):
    ny,nx=y+dy[i],x+dx[i]
    if 0<=ny<h and 0<=nx<w and dist[ny][nx]==-1 and s[ny][nx]=='.':
      dist[ny][nx]=dist[y][x]+1
      q.append((ny,nx))
if dist[-1][-1]==-1:
  print(-1)
else:
  print(cntW-dist[-1][-1]-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
cntW=0
for i in range(h):
  for j in range(w):
    if s[i][j]=='.':
      cntW+=1
dist=[[-1]*w for i in range(h)]
dist[0][0]=0
q=[(0,0)]
q=deque(q)
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while q:
  y,x=q.popleft()
  for i in range(4):
    ny,nx=y+dy[i],x+dx[i]
    if 0<=ny<h and 0<=nx<w and dist[ny][nx]==-1 and s[ny][nx]=='.':
      dist[ny][nx]=dist[y][x]+1
      q.append((ny,nx))
print(cntW-dist[-1][-1]-1)

Submission status: Wrong Answer

Solution code:
R, C = list(map(int, input().split()))

dist = [[0] * C for i in range(R)]

count_white = 0

for i in range(R):
    c_str = input()
    for j in range(len(c_str)):
        if c_str[j]=='#':
            dist[i][j] = -2
        else:
            dist[i][j] = -1
            count_white += 1

def check(x, y):
    global dist
    
    if x < 0:
        return False
    if x >= len(dist):
        return False
    if y < 0:
        return False
    if y >= len(dist[0]):
        return False
    
    if dist[x][y]!=-1:
        return False
    
    return True
    
            
#start
q = [[0, 0]]
dist[0][0] = 0

while(q):
    x = q[0][0]
    y = q[0][1]
    
    if check(x-1,y):
        dist[x-1][y] = dist[x][y] + 1
        q.append([x-1, y])
    if check(x+1,y):
        dist[x+1][y] = dist[x][y] + 1
        q.append([x+1, y])
    if check(x,y-1):
        dist[x][y-1] = dist[x][y] + 1
        q.append([x, y-1])
    if check(x,y+1):
        dist[x][y+1] = dist[x][y] + 1
        q.append([x, y+1])
    
    q.pop(0)

print(count_white - dist[-1][-1] -1)

Submission status: Wrong Answer

Solution code:
R, C = list(map(int, input().split()))

dist = [[0] * C for i in range(R)]

count_white = 0

for i in range(R):
    c_str = input()
    for j in range(len(c_str)):
        if c_str[j]=='#':
            dist[i][j] = -1
        else:
            dist[i][j] = -2
            count_white += 1

def check(x, y):
    global dist
    
    if x < 0:
        return False
    if x >= len(dist):
        return False
    if y < 0:
        return False
    if y >= len(dist[0]):
        return False
    
    if dist[x][y]!=-2:
        return False
    
    return True
    
            
#start
q = [[0, 0]]
dist[0][0] = 0

while(q):
    x = q[0][0]
    y = q[0][1]
    
    if check(x-1,y):
        dist[x-1][y] = dist[x][y] + 1
        q.append([x-1, y])
    if check(x+1,y):
        dist[x+1][y] = dist[x][y] + 1
        q.append([x+1, y])
    if check(x,y-1):
        dist[x][y-1] = dist[x][y] + 1
        q.append([x, y-1])
    if check(x,y+1):
        dist[x][y+1] = dist[x][y] + 1
        q.append([x, y+1])
    
    q.pop(0)

print(count_white - dist[-1][-1] -1)

Submission status: Wrong Answer

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


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

H, W = map(int, input().split())
white = 0
w = [None]*H
for i in range(H):
    w[i] = input()
    # print(w[i])
for i in range(H):
    for j in range(W):
        if w[i][j] == '.':
            white += 1
#print("white", white)
d = deque()
d.append((0, 0))
dist = [None]*H
for i in range(H):
    dist[i] = [None]*W
dist[0][0] = 0

while(len(d) != 0):
    g, r = d.popleft()
    #print(g, r, dist[g][r])
    if w[g][r] == '.':
        if g != 0 and dist[g-1][r] == None:
            dist[g-1][r] = dist[g][r]+1
            d.append((g-1, r))
        if g != H-1 and dist[g+1][r] == None:
            dist[g+1][r] = dist[g][r]+1
            d.append((g+1, r))
        if r != 0 and dist[g][r-1] == None:
            dist[g][r-1] = dist[g][r]+1
            d.append((g, r-1))
        if r != W-1 and dist[g][r+1] == None:
            dist[g][r+1] = dist[g][r]+1
            d.append((g, r+1))
if dist[H-1][W-1] == None:
    print(-1)
else:
    print(white-(dist[g][r]+1))

Submission status: Wrong Answer

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


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

H, W = map(int, input().split())
white = 0
w = [None]*H
for i in range(H):
    w[i] = input()
    # print(w[i])
for i in range(H):
    for j in range(W):
        if w[i][j] == '.':
            white += 1
#print("white", white)
d = deque()
d.append((0, 0))
dist = [None]*H
for i in range(H):
    dist[i] = [None]*W
dist[0][0] = 0
cost = 100
while(len(d) != 0):
    g, r = d.popleft()
    #print(g, r, dist[g][r])
    if w[g][r] == '.':
        if g != 0 and dist[g-1][r] == None:
            dist[g-1][r] = dist[g][r]+1
            d.append((g-1, r))
        if g != H-1 and dist[g+1][r] == None:
            dist[g+1][r] = dist[g][r]+1
            d.append((g+1, r))
        if r != 0 and dist[g][r-1] == None:
            dist[g][r-1] = dist[g][r]+1
            d.append((g, r-1))
        if r != W-1 and dist[g][r+1] == None:
            dist[g][r+1] = dist[g][r]+1
            d.append((g, r+1))
    if H-1 == g and W-1 == r:
        cost = min(cost, dist[g][r])
        print(white-(cost+1))
        break
    if dist[g][r] == None:
        print(-1)

Submission status: Wrong Answer

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


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

H, W = map(int, input().split())
white = 0
w = [None]*H
for i in range(H):
    w[i] = input()
    # print(w[i])
for i in range(H):
    for j in range(W):
        if w[i][j] == '.':
            white += 1
#print("white", white)
d = deque()
d.append((0, 0))
dist = [None]*H
for i in range(H):
    dist[i] = [None]*W
dist[0][0] = 0
cost = 100
while(len(d) != 0):
    g, r = d.popleft()
    #print(g, r, dist[g][r])
    if w[g][r] == '.':
        if g != 0 and dist[g-1][r] == None:
            dist[g-1][r] = dist[g][r]+1
            d.append((g-1, r))
        if g != H-1 and dist[g+1][r] == None:
            dist[g+1][r] = dist[g][r]+1
            d.append((g+1, r))
        if r != 0 and dist[g][r-1] == None:
            dist[g][r-1] = dist[g][r]+1
            d.append((g, r-1))
        if r != W-1 and dist[g][r+1] == None:
            dist[g][r+1] = dist[g][r]+1
            d.append((g, r+1))
        if H-1 == g and W-1 == r:
            cost = min(cost, dist[g][r])
print(white-(cost+1))

Submission status: Wrong Answer

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


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

H, W = map(int, input().split())
white = 0
w = [None]*H
for i in range(H):
    w[i] = input()
    # print(w[i])
for i in range(H):
    for j in range(W):
        if w[i][j] == '.':
            white += 1
#print("white", white)
d = deque()
d.append((0, 0))
dist = [None]*H
for i in range(H):
    dist[i] = [None]*W
dist[0][0] = 0
cost = -1
while(len(d) != 0):
    g, r = d.popleft()
    #print(g, r, dist[g][r])
    cost = max(cost, dist[g][r])
    if w[g][r] == '.':
        if g != 0 and dist[g-1][r] == None:
            dist[g-1][r] = dist[g][r]+1
            d.append((g-1, r))
        if g != H-1 and dist[g+1][r] == None:
            dist[g+1][r] = dist[g][r]+1
            d.append((g+1, r))
        if r != 0 and dist[g][r-1] == None:
            dist[g][r-1] = dist[g][r]+1
            d.append((g, r-1))
        if r != W-1 and dist[g][r+1] == None:
            dist[g][r+1] = dist[g][r]+1
            d.append((g, r+1))
print(white-(cost-1))
# print(cost)

Submission status: Wrong Answer

Solution code:

from collections import deque


def submit():
    h, w = map(int, input().split())
    board = [input() for _ in range(h)]

    white = 0
    for i in range(h):
        for j in range(w):
            if board[i][j] == ".":
                white += 1
    
    q = deque()
    q.appendleft((0, 0))
    dist = {(i, j): -1 for i in range(h) for j in range(w)}
    dist[(0, 0)] = 1
    while len(q):
        x, y = q.pop()
        d = dist[(x, y)]
        if (x, y) == (h - 1, w - 1):
            break

        if 0 <= x - 1 and dist[(x - 1, y)] < 0 and board[x - 1][y] == '.':
            dist[(x - 1, y)] = d + 1
            q.appendleft((x - 1, y))
        if x + 1 < h and dist[(x + 1, y)] < 0 and board[x + 1][y] == '.':
            dist[(x + 1, y)] = d + 1
            q.appendleft((x + 1, y))
        if 0 <= y - 1 and dist[(x, y - 1)] < 0 and board[x][y - 1] == '.':
            dist[(x, y - 1)] = d + 1
            q.appendleft((x, y - 1))
        if y + 1 < w and dist[(x, y + 1)] < 0 and board[x][y + 1] == '.':
            dist[(x, y + 1)] = d + 1
            q.appendleft((x, y + 1))

    if dist[(h - 1, w - 1)] < 0:
        print(-1)
    print(white - dist[(h - 1, w - 1)])

    
submit()

Submission status: Wrong Answer

Solution code:

from collections import deque


def submit():
    h, w = map(int, input().split())
    board = [input() for _ in range(h)]

    white = 0
    for i in range(h):
        for j in range(w):
            if board[i][j] == ".":
                white += 1
    
    q = deque()
    q.appendleft((0, 0))
    dist = {(i, j): -1 for i in range(h) for j in range(w)}
    dist[(0, 0)] = 1
    while True:
        x, y = q.pop()
        d = dist[(x, y)]
        if (x, y) == (h - 1, w - 1):
            break

        if 0 <= x - 1 and dist[(x - 1, y)] < 0:
            dist[(x - 1, y)] = d + 1
            q.appendleft((x - 1, y))
        if x + 1 < h and dist[(x + 1, y)] < 0:
            dist[(x + 1, y)] = d + 1
            q.appendleft((x + 1, y))
        if 0 <= y - 1 and dist[(x, y - 1)] < 0:
            dist[(x, y - 1)] = d + 1
            q.appendleft((x, y - 1))
        if y + 1 < w and dist[(x, y + 1)] < 0:
            dist[(x, y + 1)] = d + 1
            q.appendleft((x, y + 1))

    print(white - dist[(h - 1, w - 1)])

    
submit()

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline

from collections import deque
import copy

def myinput():
    return map(int,readline().split())

def mycol(data,col):
    return [ row[col] for row in data ]

h,w = myinput()

maze = [ list(input()) for _ in range(h) ]
# print(maze)

white = 0
black = 0
for i in range(h):
    for j in range(w):
        s = maze[i][j]
        if s==".":
            white += 1
        elif s=="#":
            black += 1
        else:
            pass
# print(white)
# print(black)

def bfs(sy,sx):
    q = deque()
    q.append([sy,sx])
    d = [ [float("inf")]*w for _ in range(h) ]
    d[sy][sx] = 0
    while q:
        y,x = q.popleft()
        if y==(h-1) and x==(w-1):
            break
        else:
            for i,j in ([0,1],[1,0],[-1,0],[0,-1]):
                ny = y + i
                nx = x + j
                if ny==-1 or nx==-1 or ny==h or nx==w:
                    pass
                elif maze[ny][nx]=="#":
                    pass
                elif d[ny][nx]!=float("inf"):
                    pass
                else:
                    d[ny][nx] = d[y][x] + 1
                    q.append([ny,nx])
    return d[h-1][w-1]
    
path = bfs(0,0)
# print(path)

ans = white - ( path + 1 )
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline

from collections import deque
import copy

def myinput():
    return map(int,readline().split())

def mycol(data,col):
    return [ row[col] for row in data ]

h,w = myinput()

maze = [ list(input()) for _ in range(h) ]
# print(maze)

white = 0
black = 0
for i in range(h):
    for j in range(w):
        s = maze[i][j]
        if s==".":
            white += 1
        elif s=="#":
            black += 1
        else:
            pass
# print(white)
# print(black)

def bfs(sy,sx):
    q = deque()
    q.append([sy,sx])
    d = [ [float("inf")]*w for _ in range(h) ]
    d[sy][sx] = 0
    while q:
        y,x = q.popleft()
        if y==(h-1) and x==(w-1):
            break
        for i,j in ([0,1],[1,0],[-1,0],[0,-1]):
            ny = y + i
            nx = x + j
            if ny==-1 or nx==-1 or ny==h or nx==w:
                pass
            elif maze[ny][nx]=="#":
                pass
            elif d[ny][nx]!=float("inf"):
                pass
            else:
                d[ny][nx] = d[y][x] + 1
                q.append([ny,nx])
    return d[h-1][w-1]
    
path = bfs(0,0)
# print(path)

ans = white - ( path + 1 )
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline

from collections import deque
import copy

def myinput():
    return map(int,readline().split())

def mycol(data,col):
    return [ row[col] for row in data ]

h,w = myinput()

maze = [ list(input()) for _ in range(h) ]
# print(maze)

white = 0
black = 0
for i in range(h):
    for j in range(w):
        s = maze[i][j]
        if s==".":
            white += 1
        elif s=="#":
            black += 1
        else:
            pass
# print(white)
# print(black)

def bfs(sy,sx):
    q = deque()
    q.append([sy,sx])
    d = [ [float("inf")]*w for _ in range(h) ]
    d[sy][sx] = 0
    while q:
        y,x = q.popleft()
        if y==(h-1) and x==(w-1):
            return d[y][x]
        for i,j in ([0,1],[1,0],[-1,0],[0,-1]):
            ny = y + i
            nx = x + j
            if ny==-1 or nx==-1 or ny==h or nx==w:
                pass
            elif maze[ny][nx]=="#":
                pass
            elif d[ny][nx]!=float("inf"):
                pass
            else:
                d[ny][nx] = d[y][x] + 1
                q.append([ny,nx])
    return d[h-1][w-1]
    
path = bfs(0,0)
# print(path)

ans = white - ( path + 1 )
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

H,W = map(int,input().split())
maze = [list(input())for _ in range(H)]
count = 0
for i in range(H):
  for j in range(W):
    if maze[i][j]==".":
      count += 1

maze[0][0]=0

patterns = [[1,0],[-1,0],[0,1],[0,-1]]
que=deque([[0,0]])
while que:
  now = que.popleft()
  nowY = now[0]
  nowX = now[1]
  nowstep = maze[nowY][nowX]
  if nowY == H-1 and nowX == W-1:
    break
  for pattern in patterns:
    nextY = nowY + pattern[0]
    nextX = nowX + pattern[1]
    nextstep = nowstep + 1
    if 0<=nextY<H and 0<=nextX<W and maze[nextY][nextX]==".":
      que.append([nextY,nextX])
      maze[nextY][nextX]=nextstep
      
print(count-nowstep-1)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

from collections import deque
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

H, W = na()
g = [ns() for _ in range(H)]
d = [[-1] * W for _ in range(H)]
ct = 0
for i in range(H):
    ct += g[i].count('#')
d[0][0] = 1
que = deque()
que.append((0, 0))
while que:
    y, x = que.popleft()
    if y == H - 1 and x == W - 1:
        break
    for dy, dx in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        ny = y + dy
        nx = x + dx
        if 0 <= ny < H and 0 <= nx < W and g[ny][nx] != '#' and d[ny][nx] == -1:
            d[ny][nx] = d[y][x] + 1
            que.append((ny, nx))
            
print(H * W - ct - d[H-1][W-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


H, W = inpl()
S = ["#" + input().strip() + "#" for _ in range(H)]
S = ["#" * (W + 2)] + S + ["#" * (W + 2)]
sharp = sum(len(list(filter(lambda x: x == "#", s))) for s in S)
sharp -= 2 * H + (W + 2) * 2

mp = [[-1] * (W + 2) for i in range(H + 2)]
mp[1][1] = 0
Q = deque()
Q.append((1, 1))
dhw = [(1, 0), (0, 1), (-1, 0), (0, -1)]
while Q:
    h, w = Q.popleft()
    for dh, dw in dhw:
        if S[h + dh][w + dw] == "#":
            continue
        elif mp[h + dh][w + dw] != -1:
            continue
        mp[h + dh][w + dw] = mp[h][w] + 1
        Q.append((h + dh, w + dw))

print(H * W - mp[H][W] - 1 - sharp)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
inf=1000000000000000
d=[[inf]*w for i in range(h)]


def dfs(i,j):
    global h,w,s
    nex=[(0,-1),(0,1),(-1,0),(1,0)]
    for k in range(4):
        l,m=i+nex[k][0],j+nex[k][1]
        if 0<=i+nex[k][0]<h and 0<=j+nex[k][1]<w:
            if s[l][m]=="." and d[l][m]>d[i][j]+1:
                d[l][m]=d[i][j]+1
                dfs(l,m)
d[0][0]=0
dfs(0,0)
ans=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            ans+=1

print(ans-d[h-1][w-1]-1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

from collections import deque

H, W = map(int, input().split())

field = []
for _ in range(H):
    line = list(input())
    field.append(line)
# print(H, W, field)


def is_valid(i, j):
    cond_i = 0 <= i <= H - 1
    cond_j = 0 <= j <= W - 1
    return cond_i and cond_j


def adj(i, j):
    up, down = [i - 1, j], [i + 1, j]
    left, right = [i, j - 1], [i, j + 1]
    res = []
    for i_adj, j_adj in up, down, left, right:
        if is_valid(i_adj, j_adj):
            res.append([i_adj, j_adj])
    return res


def bfs(que, dist):
    while len(que) > 0:
        i, j = que.popleft()
        # print(f"i, j: {i}, {j}")
        for i_adj, j_adj in adj(i, j):
            # print(f"i_adj, j_adj: {i_adj}, {j_adj}")
            if dist[(i_adj, j_adj)] == -1:
                que.append([i_adj, j_adj])
                dist[(i_adj, j_adj)] = dist[(i, j)] + 1


que = deque()
keys = [(i, j) for i in range(H) for j in range(W)]
values = [-1 for _ in range(H * W)]
dist = dict(zip(keys, values))
# print(dist)

n_black = 0
for i in range(H):
    for j in range(W):
        if field[i][j] == '#':
            n_black += 1

que.append([0, 0])
dist[(0, 0)] = 0
bfs(que, dist)

if dist[(H - 1, W - 1)] == -1:
    print(-1)
else:
    steps_min = dist[(H - 1, W - 1)] + 1
    n_white = W * H - n_black
    ans = n_white - steps_min
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

from collections import deque

H, W = map(int, input().split())

field = []
for _ in range(H):
    line = list(input())
    field.append(line)
# print(H, W, field)


def is_valid(i, j):
    cond_i = i >= 0 and i <= H - 1
    cond_j = j >= 0 and j <= W - 1
    return cond_i and cond_j


def adj(i, j):
    up, down = [i - 1, j], [i + 1, j]
    left, right = [i, j - 1], [i, j + 1]
    res = []
    for i_adj, j_adj in up, down, left, right:
        if is_valid(i_adj, j_adj):
            res.append([i_adj, j_adj])
    return res


def bfs(que, dist):
    while len(que) > 0:
        i, j = que.popleft()
        # print(f"i, j: {i}, {j}")
        for i_adj, j_adj in adj(i, j):
            # print(f"i_adj, j_adj: {i_adj}, {j_adj}")
            if dist[(i_adj, j_adj)] == -1:
                que.append([i_adj, j_adj])
                dist[(i_adj, j_adj)] = dist[(i, j)] + 1


que = deque()
keys = [(i, j) for i in range(H) for j in range(W)]
values = [-1 for _ in range(H * W)]
dist = dict(zip(keys, values))
# print(dist)

n_black = 0
for i in range(H):
    for j in range(W):
        if field[i][j] == '#':
            n_black += 1

que.append([0, 0])
dist[(0, 0)] = 0
bfs(que, dist)

if dist[(H - 1, W - 1)] == -1:
    print('-1')
else:
    steps_min = dist[(H - 1, W - 1)] + 1
    n_white = W * H - n_black
    ans = n_white - steps_min
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

from collections import deque

H, W = map(int, input().split())

field = []
for _ in range(H):
    line = list(input())
    field.append(line)
# print(H, W, field)


def is_valid(i, j):
    cond_i = i >= 0 and i <= H - 1
    cond_j = j >= 0 and j <= W - 1
    return cond_i and cond_j


def adj(i, j):
    up, down = [i - 1, j], [i + 1, j]
    left, right = [i, j - 1], [i, j + 1]
    res = []
    for i_adj, j_adj in up, down, left, right:
        if is_valid(i_adj, j_adj):
            res.append([i_adj, j_adj])
    return res


def bfs(que, dist):
    while len(que) > 0:
        i, j = que.popleft()
        # print(f"i, j: {i}, {j}")
        for i_adj, j_adj in adj(i, j):
            # print(f"i_adj, j_adj: {i_adj}, {j_adj}")
            if dist[(i_adj, j_adj)] == -1:
                que.append([i_adj, j_adj])
                dist[(i_adj, j_adj)] = dist[(i, j)] + 1


que = deque()
keys = [(i, j) for i in range(H) for j in range(W)]
values = [-1 for _ in range(H * W)]
dist = dict(zip(keys, values))
# print(dist)

n_black = 0
for i in range(H):
    for j in range(W):
        if field[i][j] == '#':
            n_black += 1

que.append([0, 0])
dist[(0, 0)] = 0
bfs(que, dist)

if dist[(H - 1, W - 1)] == -1:
    print(-1)
else:
    steps_min = dist[(H - 1, W - 1)] + 1
    n_white = W * H - n_black
    ans = n_white - steps_min
    print(ans)