Problem:


Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13



Sample input:
4 6
#..#..
.....#
....#.
#.#...


Sample output:
8

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
lst = [['#'] * (W+2)]
lst += [['#'] + list(input()) + ['#'] for i in range(H)]
lst += [['#'] * (W+2)]


# print (lst)
lst_ue = [[-1] * (W+2) for i in range(H+2)]
lst_shita = [[-1] * (W+2) for i in range(H+2)]
lst_migi = [[-1] * (W+2) for i in range(H+2)]
lst_hidari = [[-1] * (W+2) for i in range(H+2)]

# print (lst_ue)
def ue(i, j):
    if lst_ue[i][j] != -1:
        return lst_ue[i][j]
    if lst[i][j] == '#':
        lst_ue[i][j] = 0
        return 0
    else:
        lst_ue[i][j] = ue(i-1, j) + 1
        return lst_ue[i][j]


def shita(i, j):
    if lst_shita[i][j] != -1:
        return lst_shita[i][j]
    if lst[i][j] == '#':
        lst_shita[i][j] = 0
        return 0
    else:
        lst_shita[i][j] = shita(i+1, j) + 1
        return lst_shita[i][j]        


def hidari(i, j):
    if lst_hidari[i][j] != -1:
        return lst_hidari[i][j]
    if lst[i][j] == '#':
        lst_hidari[i][j] = 0
        return 0
    else:
        lst_hidari[i][j] = hidari(i, j-1) + 1
        return lst_hidari[i][j]        

def migi(i, j):
    if lst_migi[i][j] != -1:
        return lst_migi[i][j]
    if lst[i][j] == '#':
        lst_migi[i][j] = 0
        return 0
    else:
        lst_migi[i][j] = migi(i, j+1) + 1
        return lst_migi[i][j]        

# def shita(i, j):
#     count = 0
#     for k in range(1, H+1):
#         if lst[i+k][j] == '#':
#             return count
#         else:
#             count += 1

# def hidari(i, j):
#     count = 0
#     for k in range(1,W+1):
#         if lst[i][j-k] == '#':
#             return count
#         else:
#             count += 1

# def migi(i, j):
#     count = 0
#     for k in range(1,W+1):
#         if lst[i][j+k] == '#':
#             return count
#         else:
#             count += 1

ans = 0
for i in range(1,H+1):
    for j in range(1, W+1):
        if lst[i][j] != '#':
            # print (i,j, 1 + ue(i,j)+shita(i,j)+hidari(i,j)+migi(i,j))
            ans = max(ans, ue(i,j)+shita(i,j)+hidari(i,j)+migi(i,j)-3)

print (ans)
# print (lst_ue)

Submission status: Runtime Error

Solution code:
def find_longest(s, n):
    tmp = 0
    for line in s:
        obs = []
        for i, tok in enumerate(line):
            if tok == '#':
                obs.append(i)
        if obs:
            diff = [obs[i + 1] - obs[i] - 1 for i in range(len(obs) - 1)]
            diff.append(obs[0])
            diff.append(n-obs[-1]-1)
        else:
            return n
        m = max(diff)
        if tmp < m:
            tmp = m
    return tmp

h, w = map(int, input().split())
s_w = []
for i in range(h):
    s_w.append(list(input()))
# print(s_w)
s_h = [row[i] for i in range(h) for row in s_w]

print(find_longest(s_h, h) + find_longest(s_w, w) -1)

Submission status: Runtime Error

Solution code:

# s = input()
# n = int(input())
# A = list(map(int, input().split()))
H, W = map(int, input().split())
S = []
vertical = []
horizontal = []

for _ in range(H):
    s = list(input())
    S.append(s)
    vertical.append([-1]*W)
    horizontal.append([-1]*W)

def search_vertical(i, j, current):
    if i == H-1 or S[i+1][j] == '#':
        vertical[i][j] = current
        return current
    vertical[i][j] = search_vertical(i+1, j, current+1)
    return vertical[i][j]

def search_horizontal(i, j, current):
    if j == W-1 or S[i][j+1] == '#':
        horizontal[i][j] = current
        return current
    horizontal[i][j] = search_horizontal(i, j+1, current+1)
    return horizontal[i][j]

for i in range(H):
    for j in range(W):
        if vertical[i][j] == -1 and S[i][j] == '.':
            search_vertical(i, j, 1)
        if horizontal[i][j] == -1 and S[i][j] == '.':
            search_horizontal(i, j, 1)


print(max(vertical[i][j] + horizontal[i][j] -1 for i in range(H) for j in range(W)))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

def f(h,w,sl):
    cnt = [[[0,0] for _ in range(w)] for _ in range(h)]

    for hi in range(h):
        wi = 0
        while wi<w:
            if sl[hi][wi]==".":
                c = 0
                wj = wi
                while wj<w and sl[hi][wj]==".":
                    c += 1
                    wj += 1
                for j in range(wi,wj):
                    cnt[hi][j][0] = c
                wi = wj
            else:
                wi += 1

    for wi in range(h):
        hi = 0
        while hi<h:
            if sl[hi][wi]==".":
                c = 0
                hj = hi
                while hj<h and sl[hj][wi]==".":
                    c += 1
                    hj += 1
                for j in range(hi,hj):
                    cnt[j][wi][1] = c
                hi = hj
            else:
                hi += 1

    res = -1
    for hi in range(h):
        for wi in range(w):
            if sl[hi][wi]==".":
                r = sum(cnt[hi][wi])
                res = max(res, r)

    return res-1


if __name__ == '__main__':
    h,w = map(int, input().split())
    sl = [input() for _ in range(h)]
    print(f(h,w,sl))

    # from random import choice
    # for _ in range(1):
    #     h = 2000
    #     w = 2000
    #     sl = ["".join([choice([".","#"]) for _ in range(w)]) for _ in range(h)]

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import defaultdict
from functools import reduce

# s = input()
# n = int(input())
# A = list(map(int, input().split()))
H, W = map(int, input().split())
S = []
vertical = []
horizontal = []

for _ in range(H):
    s = list(input())
    S.append(s)
    vertical.append([-1]*W)
    horizontal.append([-1]*W)

def search_vertical(i, j, current):
    if i == H-1 or S[i+1][j] == '#':
        vertical[i][j] = current
        return current
    vertical[i][j] = search_vertical(i+1, j, current+1)
    return vertical[i][j]

def search_horizontal(i, j, current):
    if j == W-1 or S[i][j+1] == '#':
        horizontal[i][j] = current
        return current
    horizontal[i][j] = search_horizontal(i, j+1, current+1)
    return horizontal[i][j]

for i in range(H):
    for j in range(W):
        if vertical[i][j] == -1 and S[i][j] == '.':
            search_vertical(i, j, 1)
        if horizontal[i][j] == -1 and S[i][j] == '.':
            search_horizontal(i, j, 1)


print(max(vertical[i][j] + horizontal[i][j] -1 for i in range(H) for j in range(W)))

Submission status: Runtime Error

Solution code:

nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

H, W = nl()
grid = []
for i in range(H):
    grid.append(s())

yoko = [[0] * W for h in range(H)]
for row in range(H):
    prev = 0
    count = 0
    for col in range(W):
            for i in range(prev, col):
                yoko[row][i] = count
            prev = col+1
            count = 0
        else:
            count += 1
    for i in range(prev, W):
        yoko[row][i] = count

tate = [[0] * W for h in range(H)]
for col in range(W):
    prev = 0
    count = 0
    for row in range(H):
            for i in range(prev, row):
                tate[i][col] = count
            prev = col+1
            count = 0
        else:
            count += 1
    for i in range(prev, H):
        tate[i][col] = count

res = 0
for row in range(H):
    for col in range(W):
        res = max(res, yoko[row][col] + tate[row][col] - 1)

print(res)

Submission status: Runtime Error

Solution code:
#include <math.h>
#include <bits/stdc++.h>
#include <map>
#include <set>
#include <iostream>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

using namespace std;

int main()
{
    int H,W;
    cin>>H>>W;
    string s;
    vector<string> MAP(H + 2);
    MAP[0] = '#' * (W + 2);
    MAP[H + 1] = '#' * (W + 2);
    FOR(i,1,H+1){
        cin >> s;
        MAP[i] = '#' + s + '#';
    }
    vector<vector<int>> yoko(H + 2, vector<int>(W + 2, 0));
    vector<vector<int>> tate(W + 2, vector<int>(H + 2, 0));
    FOR(w,1,1+W){
        if (MAP[1][w]=='.'){
            tate[w][1] = 1;
        }
    }
    FOR(h,1,1+H){
        if (MAP[h][1]=='.'){
            yoko[h][1] = 1;
        }
        FOR(w,1,1+W){
            if (MAP[h][w+1]=='.'){
                yoko[h][w + 1] = yoko[h][w] + 1;
            }
            else{
                int x = yoko[h][w];
                FOR(i,w-x+1,w+1){
                    yoko[h][i] = x;
                }
            }
            if (MAP[h+1][w]=='.'){
                tate[w][h + 1] = tate[w][h] + 1;
            }
            else{
                int x = tate[w][h];
                FOR(i,h-x+1,h+1){
                    tate[w][i] = x;
                }
            }
        }
    }
    int ans = 0;
    FOR(h,1,1+H){
        FOR(w,1,1+W){
            ans=max(ans,yoko[h][w]+tate[w][h]-1);
        }
    }
    cout<<ans<<endl;
}

Submission status: Runtime Error

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

area_yoko = []
area_tate = []

for i in range(H):
	tmp = []
	yoko = S[i]
	area_start = 0
	kabe = yoko[0] == "#"
	j = 0
	while j < W:
		if yoko[j] == ".":
			if kabe:
				kabe = False
				area_start = j
			j += 1
			continue
		else:
			if not kabe:
				kabe = True
				length = j - area_start
				tmp.append((area_start, j-1, length))
			j += 1
			continue
	if not kabe:
		tmp.append((area_start, W-1, W-area_start))
	area_yoko.append(tmp)


for i in range(W):
	tmp = []
	tate = [S[j][i] for j in range(H)]
	area_start = 0
	kabe = tate[0] == "#"
	j = 0
	while j < H:
		if tate[j] == ".":
			if kabe:
				kabe = False
				area_start = j
			j += 1
			continue
		else:
			if not kabe:
				kabe = True
				length = j - area_start
				tmp.append((area_start, j-1, length))
			j += 1
			continue
	if not kabe:
		tmp.append((area_start, H-1, H-area_start))
	area_tate.append(tmp)

def search(x, area):
	l = 0
	r = len(area)
	while r-l > 1:
		p = (l+r)//2
		lower = area[p][0]
		upper = area[p][1]
		if lower <= x and x <= upper:
			return area[p]
		elif x < lower:
			r = p
		else:
			l = p
	lower,upper = area[l][0], area[l][1]
	if lower <= x and x <= upper:
		return area[l]
	else:
		return None
ans = 0
for y in range(H):
	for x in range(W):
		yoko_info = search(x, area_yoko[y])
		tate_info = search(y, area_tate[x])
		if yoko_info != None and tate_info != None:
			ans = max(ans, yoko_info[2] + tate_info[2] - 1)
print(ans)

Submission status: Runtime Error

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

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

result = 0
#memo = {}


def terasu(x, y, d):
    #print(x, y, h, w)
    if y < 0 or x < 0 or y >= h or x >= w:
        return 0
    # if (x, y) in memo.keys():
    #    return 0
    if s[y][x] == "#":
        return 0
    #memo[(x, y)] = True
    if d == "f":
        return 1 + terasu(x + 1, y, "e") + terasu(x, y - 1, "s") + terasu(x, y+1, "n") + terasu(x-1, y, "w")
    elif d == "e":
        return 1 + terasu(x + 1, y, "e")
    elif d == "s":
        return 1 + terasu(x, y - 1, "s")
    elif d == "n":
        return 1 + terasu(x, y+1, "n")
    elif d == "w":
        return 1 + terasu(x-1, y, "w")


for y in range(h):
    for x in range(w):
        result = max(result, terasu(x, y, "f"))
print(result)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
a = [0 for i in range(n)]
for i in range(n):
  a[i] = input()
num = [[0 for j in range(m)] for i in range(n)]
for i in range(n):
  for j in range(m):
    if a[i][j] == ".":
      num[i][j] += 1
      # 下
      i_2 = i+1
      j_2 = j
      if i_2 < n:
        while a[i_2][j_2] == ".":
          num[i][j] += 1
          i_2 += 1
          if i_2 < n:
            1
          else:
            break
      # 上
      i_2 = i-1
      j_2 = j
      if 0 <= i_2:
        while a[i_2][j_2] == ".":
          num[i][j] += 1
          i_2 -= 1
          if 0 <= i_2:
            1
          else:
            break
      # 上
      i_2 = i
      j_2 = j-1
      if 0 <= j_2m:
        while a[i_2][j_2] == ".":
          num[i][j] += 1
          j_2 -= 1
          if 0 <= j_2:
            1
          else:
            break
      # 下
      i_2 = i
      j_2 = j+1
      if j_2 < m:
        while a[i_2][j_2] == ".":
          num[i][j] += 1
          j_2 += 1
          if j_2 < m:
            1
          else:
            break
print(max([max(i) for i in num]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[input() for _ in range(h)]
yoko_map=[[0 for i in range(w)] for j in range(h)]
tate_map=[[0 for i in range(w)] for j in range(h)]
for i in range(h):
    p=0
    s_=s[i]
    s_=s_.split('#')
    for x in s_:
        num=len(x)
        for j in range(num):
            yoko_map[i][p]=num
            p+=1
        p+=1
for i in range(h):
    p=0
    s_=''.join([s__[i] for s__ in s])
    s_=s_.split('#')
    for x in s_:
        num=len(x)
        for j in range(num):
            tate_map[p][i]=num
            p+=1
        p+=1
m=0
for i in range(h):
    yoko_=yoko_map[i]
    tate_=tate_map[i]
    for j in range(w):
        m=max(m,tate_[j]+yoko_[j])
print(m-1)

Submission status: Runtime Error

Solution code:
h, w = li()
grid = [lc() for _ in range(h)]

def left_right(grid, row, col, cnt, w):
    if col == w:
        return cnt

    elif grid[row][col] == "#":
        return cnt

    else:
        cnt = left_right(grid, row, col+1, cnt+1, w)

    lr[row][col] = cnt
    return cnt


def top_bottom(grid, row, col, cnt, h):
    if row == h:
        return cnt

    elif grid[row][col] == "#":
        return cnt

    else:
        cnt = top_bottom(grid, row+1, col, cnt+1, h)

    tb[row][col] = cnt
    return cnt

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

for row in range(h):
    for col in range(w):
        if grid[row][col] == "." and lr[row][col] == 0:
            left_right(grid, row, col, 0, w)

for col in range(w):
    for row in range(h):
        if grid[row][col] == "." and tb[row][col] == 0:
            top_bottom(grid, row, col, 0, h)

ans = 0
for row in range(h):
    for col in range(w):
        ans = max(ans, lr[row][col] + tb[row][col])

print(ans-1)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
h,w = map(int, input().split())
sl = [input() for _ in range(h)]

cnt = [[[0,0] for _ in range(w)] for _ in range(h)]

for hi in range(h):
    wi = 0
    while wi<w:
        if sl[hi][wi]==".":
            c = 0
            wj = wi
            while wj<w and sl[hi][wj]==".":
                c += 1
                wj += 1
            for j in range(wi,wj):
                cnt[hi][j][0] = c
            wi = wj
        else:
            wi += 1

for wi in range(h):
    hi = 0
    while hi<h:
        if sl[hi][wi]==".":
            c = 0
            hj = hi
            while hj<h and sl[hj][wi]==".":
                c += 1
                hj += 1
            for j in range(hi,hj):
                cnt[j][wi][1] = c
            hi = hj
        else:
            hi += 1

res = -1
for hi in range(h):
    for wi in range(w):
        if sl[hi][wi]==".":
            r = sum(cnt[hi][wi])
            res = max(res, r)

print(res-1)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())

T = ["" for i in range(W)]

xs = [[0 for i in range(W)] for i in range(H)]
ys = [[0 for i in range(W)] for i in range(H)]

for i in range(H):
	st = input()
	c = 0
	
	for j in range(W):
		T[j]=T[j]+st[j]
		if st[j] = "#":
			xc = j-c
			xs[i][j] = 0
			ys[i][j] = 0
			for k in ragne(c,j):
				xs[i][k] = xc
			c = j+1

for i in range(W):
	st = T[i]
	c = 0
	for j in range(H):
		if st[j] =  "#":
			yc = j-c
			for k in range(c,j):
				ys[k][i] = yc
			c = j+1

r = 0
for i in range(H):
	for j in range(W):
		r = max(r,xs[i][j] + ys[i][j])

print(r)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
ans = [[0 for i in range(W)] for j in range(H)]
def f(h,w,x):
  global H,W,S,ans
  if h<0 or h>=H or w<0 or w>=W or S[h][w]=="#":
    return
  ans[h][w]+=1
  if x==0:
    f(h-1,w,1)
    f(h+1,w,2)
    f(h,w-1,3)
    f(h,w+1,4)
  elif x==1:
    f(h-1,w,1)
  elif x==2:
    f(h+1,w,2)
  elif x==3:
    f(h,w-1,3)
  elif x==4:
    f(h,w+1,4)
for h in range(H):
  for w in range(W):
    if S[h][w]=="#":
      continue
    f(h,w,0)
a = 0
for h in ans:
  a = max(a,max(h))
print(a)

Submission status: Runtime Error

Solution code:
import sys, math, collections, heapq, itertools
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    H, W = map(int, file().split())
    S = [None] * H
    for i in range(H):
        S[i] = list(file())
    left, right, up, down = [[0 for i in range(W)] for j in range(H)], [[0 for i in range(W)] for j in range(H)],[[0 for i in range(W)] for j in range(H)],[[0 for i in range(W)] for j in range(H)]
    for h in range(H):
        if S[h][0] == ".": left[h][0] += 1
        if S[h][W-1] == ".": right[h][W-1] += 1
    for w in range(W):
        if S[0][w] == ".": up[0][w] += 1
        if S[0][H-1] == ".": down[H-1][w] += 1

    for h in range(H):
        for w in range(1, W):
            if S[h][w] == ".":
                left[h][w] = left[h][w-1] + 1
                right[h][W - w - 1] = right[h][W - w] + 1
    for h in range(1, H):
        for w in range(W):
                up[h][w] = up[h-1][w] + 1
                down[H-h-1][w] = down[H-h][w] + 1

    maxlight = 0
    for h in range(H):
        for w in range(W):
            maxlight = max(maxlight, left[h][w] + right[h][w] + up[h][w] + down[h][w] - 3)
    print(maxlight)      

    return 0
  
if __name__ == "__main__":
    solve()