Problem:


Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

4 4
##.#
....
##.#
.#.#

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

4 5
.....
.....
..#..
.....

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

..#
#..
.#.
.#.
#.#



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


Sample output:
###
###
.##

Submission status: Accepted

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

def solve(h,w,a):
  h_flags = [True]*h
  w_flags = [False]*w
  for i in range(h):
    if not "#" in a[i]:
      h_flags[i] = False
    for j in range(w):
      if a[i][j] == "#":
        w_flags[j] = True
  for i in range(h):
    if not h_flags[i]:
      continue
    for j in range(w):
      if w_flags[j]:
        print(a[i][j],end="")
    print("")
  return 



def main():
  H,W = map(int,input().split())
  a = [input() for _ in range(H)]
  solve(H,W,a)
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = []
for i in range(h):
    tmp = list(input())
    a.append(tmp)
H = [0] * h
W = [0] * w

b = list(zip(*a))

for i in range(h):
    if a[i].count('#') != 0:
        H[i] = 1

for i in range(w):
    if b[i].count('#') != 0:
        W[i] = 1

c = []
for i in range(h):
    tmp = []
    for j in range(w):
        if H[i] == 1 and W[j] == 1:
            tmp.append(a[i][j])
    if tmp != []:
        c.append(tmp)

for i in range(len(c)):
    print(''.join(c[i]))

Submission status: Accepted

Solution code:
import math
def main():
  h,w = list(map(int,input().split()))
  a = [input() for i in range(h)]
  ans=0
  i=0
  while i<h:
    if '#' not in a[i] :
      a.pop(i)
      h=h-1
      i-=1
    i+=1
  i=0
  while i<w:
    for j in range(0,h):
      if a[j][i]=='#':
        break
      elif j==h-1:
        for k in range(0,h):
          a[k]=a[k][:i]+a[k][i+1:]
        w-=1
        i-=1
    i+=1
  for i in range(0,h):
    print(a[i])
main()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
f=lambda S,L:"".join(map(lambda x:S[x],L))
S=[]

B=[]
for w in range(H):
    K=input()
    S.append(K)

    if K.count("#"):
        B.append(w)
        

A=[]
for x in range(W):
    F=False
    for y in range(H):
        F|=(S[y][x]=="#")

    if F:
        A.append(x)
        
T=[]
for b in B:
    T.append(f(S[b],A))
print("\n".join(T))

Submission status: Accepted

Solution code:
def elimrow(lst):
    lst_after = []
    gset = []
    H1 = len(lst)
    for i in range(H1):
        gset = list(set(lst[i]))
        gset.sort()
        #print(gset)
        if gset[0] != '.':
            lst_after.append(lst[i])
    return lst_after

def trans(lst):
    return list(zip(*lst))

def main():
    H,W = map(int,input().split())

    grid = [list(str(input())) for _ in range(H)]
    grid_after = []
    grid_after = elimrow(grid)
    gridT = trans(grid_after)
    grid_after2 = elimrow(gridT)
    gridT2 = trans(grid_after2)
    #print(gridT2)
    for i in gridT2:
        print(*i,sep='')

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    h, w = map(int, input().split())
    a = []
    row_ng = ['.'] * w
    for _ in range(h):
        row = list(input())
        if row == row_ng:
            h -= 1
        else:
            a.append(row)

    a2 = [[] for _ in range(h)]
    for i1 in range(w):
        flag = False
        for i2 in range(h):
            if a[i2][i1] == '#':
                flag = True
                break
        if flag:
            for i3 in range(h):
                a2[i3].append(a[i3][i1])

    for arow in a2:
        print("".join(arow))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
ch = []
for i in range(h):
    a = str(input())
    if "#" in a:
        ch.append(a)
h = len(ch)
bit = [0]*w
for i in range(h):
    for j in range(w):
        if ch[i][j] == "#":
            bit[j] = 1

for i in range(h):
    ans = ""
    for j in range(w):
        ans += ch[i][j]*bit[j]
    print(ans)

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
maps = [None]*h
for i in range(h):
    maps[i] = input()
hashes = []
for i in range(h):
    if "#" in maps[i]:
        hashes.append(i)
white = []
for i in range(w):
    flag = 0
    for j in range(h):
        if maps[j][i] == "#":
            flag = 1
    if flag == 1:
        white.append(i)
for i in hashes:
    for j in white:
        print(maps[i][j],end="")
    print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
grid = [list(input()) for i in range(h)]
row = {}
col = {}

for i in range(h):
    if "#" in grid[i]:
        row[i] = True
    else:
        row[i] = False

for j in range(w):
    c = False
    for i in range(h):
        if grid[i][j] == "#":
            c = True
    else:
        if c:
            col[j] = True
        else:
            col[j] = False

for i in range(h):
    o = False
    for j in range(w):
        if row[i] and col[j]:
            print (grid[i][j], end="")
            o = True
            pass
        else:
            pass
    if o:
        print ()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
Hs = set()
Ws = set()
for h in range(H):
  for w in range(W):
    if grid[h][w] == "#":
      Hs.add(h)
      Ws.add(w)
Hs = sorted(list(Hs))
Ws = sorted(list(Ws))
for h in Hs:
  for w in Ws:
    print(grid[h][w], end="")
  print()

Submission status: Accepted

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

#行の削除
for i in range(len(A)-1, -1, -1):
  if A[i].count(".") == len(A[i]):
        A.pop(i)

rm = []
#列の削除
for w in range(W):
  f = True
  for h in range(len(A)):
    if A[h][w] == "#":
        f = False
  if f:
    rm.append(w)

for r in range(len(rm)-1, -1, -1):
  for h in range(len(A)):
      A[h] = A[h][:rm[r]] + A[h][rm[r]+1:]

#出力
for a in A:
  print(a)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
A = [list(input()) for i in range(H)]
hi_list = []
wj_list = []
for i in range(H):
    c = 0
    for j in range(W):
        if A[i][j] == '.':
            c += 1
    if c == W:
        hi_list += [i]
for j in range(W):
    c = 0
    for i in range(H):
        if A[i][j] == '.':
            c += 1
    if c == H:
        wj_list += [j]

hi_set = set(hi_list)
wj_set = set(wj_list)
for i in range(H):
    if i in hi_set:
        continue
    for j in range(W):
        if j in wj_set:
            continue
        print(A[i][j], end='')
    print()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
L = []
for h in range(H):
  L.append(input())
  
X = [0]*W
Y = [0]*H

for h in range(H):
  for w in range(W):
    if L[h][w]=="#":
      X[w]=1
      Y[h]=1
      
for h in range(H):
  if Y[h]==1:
    for w in range(W):
      if X[w]==1:
        print(L[h][w] , end="")
    print()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    H, W = il()
    HW = [sl() for _ in range(H)]
    h = [False] * H
    w = [False] * W

    for n in range(H):
        hw = HW[n]
        if '#' in hw[0]:
            h[n] = True
        for m in range(W):
            if hw[0][m] == '#':
                w[m] = True

    for n in range(H):
        if h[n]:
            str = ''
            for m in range(W):
                if w[m]:
                    str += HW[n][0][m]
            print(str)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
l = []
for h in range(H):
    s = list(input())
    if s != ["."]*W:
        l.append(s)

for w in range(W-1, -1, -1):
    cnt = 0
    for h in range(len(l)):
        if l[h][w] == "#":
            break
        else:
            cnt += 1
    if cnt == len(l):
        for h in range(len(l)):
            del l[h][w]
for h in range(len(l)):
    print("".join(l[h]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]


def solve():
    n, m = [int(x) for x in input().split()]
    s = []
    ii = []
    jj = []
    cols = [[] for _ in range(m)]
    for i in range(n):
        r = [x for x in input()]
        if '#' not in set(r):
            ii.append(i)
        for j in range(m):
            cols[j].append(r[j])
        s.append(r)
    for j in range(m):
        if '#' not in set(cols[j]):
            jj.append(j)
    
    for i in range(n):
        if i in ii:
            continue
        for j in range(m):
            if j in jj:
                continue
            print(s[i][j], end='')
        print()


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()

"""

"""

Submission status: Accepted

Solution code:
H, W = [int(i) for i in input().split()]

grid = [""]*W

for _ in range(H):
    tmp = input()
    if "."*W != tmp:
        for i, x in enumerate(tmp):
            grid[i] += x
j = len(grid[0])
ans = [""]*j
for row in grid:
    if "."*j != row:
        for i, s in enumerate(row):
            ans[i] += s


for r in ans:
    print(r)

Submission status: Accepted

Solution code:
import numpy as np

H, W, *M = open(0).read().split()
H = int(H)
W = int(W)
M = np.array([list(r) for r in M])

del_rows = []

M_ans = M

for i in range(H):
    if "#" not in M_ans[i]:
         del_rows.append(i)
            
M_ans = np.delete(M_ans, del_rows, 0)

del_cols = []

for j in range(len(M_ans.T)):
    if "#" not in M_ans.T[j]:
         del_cols.append(j)
            
M_ans = np.delete(M_ans, del_cols, 1)

print(*["".join(r) for r in M_ans], sep="\n")

Submission status: Accepted

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

row = []
col = []
for i in range(h):
  if a[i].count(".") != w: row.append(i)
for i in range(w):
  t = 0
  for j in range(h):
    if a[j][i] == ".": t+=1
  if t != h: col.append(i)

for i in row:
  ans = ""
  for j in col:
    ans += a[i][j]
  print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
loop_flag = True
while loop_flag:
    loop_flag = False

    for i in range(h):
        f = True
        for j in range(w):
            if a[i][j] != ".":
                f = False
                break
                print(i, "breaked")
        if f:
            loop_flag = True
            a.pop(i)
            h -= 1
            break
            
    #print(a, h, w)

    for j in range(w):
        f = True 
        for i in range(h):
            # print(i, j, a[i][j])
            if a[i][j] != ".":
                f = False
                break
        if f:
            loop_flag = True
            for i in range(h):
                a[i].pop(j)
            w -= 1
            break
        
for i in range(h):
    print("".join(a[i]))

Submission status: Accepted

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

a = []
for i in range(H):
    a.append(list(input()))

line = []
colu = []
for i in range(H):
    for j in range(W):
        if a[i][j] == '#':
            line.append(i)
            colu.append(j)

ans=[]
for i in range(H):
    dammy=[]
    for j in range(W):
        if (i in line) and (j in colu):
            dammy.append(a[i][j])
    if dammy!=[]:
        ans.append(dammy)

for i in range(len(ans)):
    print(''.join(ans[i]))

Submission status: Accepted

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

a = []
for i in range(H):
    a.append(list(input()))

line = [0]*H
colu = [0]*W
for i in range(H):
    for j in range(W):
        if a[i][j] == '#':
            line[i]=1
            colu[j]=1

ans=[]
for i in range(H):
    dammy=[]
    for j in range(W):
        if line[i]==1 and colu[j]==1 :
            dammy.append(a[i][j])
    if dammy!=[]:
        ans.append(dammy)

for i in range(len(ans)):
    print(''.join(ans[i]))

Submission status: Accepted

Solution code:
H,W= list(map(int,input().split()))
M = []

for i in range(H):
    M.append(list(input()))

Hs = []
for i in range(H):
    flag = 0
    for j in range(W):
        if M[i][j]!=".":
            flag = 1
    if flag==1:
        Hs.append(i)

Ws = []
for j in range(W):
    flag = 0
    for i in range(H):
        if M[i][j]!=".":
            flag = 1
    if flag==1:
        Ws.append(j)

for i in Hs:
    out = []
    for j in Ws:
        out.append(M[i][j])
    print("".join(out))

Submission status: Accepted

Solution code:

[H,W] = list(map(int,input().split()))

a = []
for i in range(H):
    a.append(list(input()))

# print(*a, sep='\n')

line = []
colu = []
for i in range(H):
    for j in range(W):
        if a[i][j] == '#':
            line.append(i)
            colu.append(j)
# print('line:', line)
# print('culu:', colu)

ans=[]
for i in range(H):
    dammy=[]
    for j in range(W):
        if (i in line) and (j in colu):
            dammy.append(a[i][j])
    if dammy!=[]:
        ans.append(dammy)

for i in range(len(ans)):
    print(''.join(ans[i]))
#print(*ans)

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().split())
board = [list(str(input())) for _ in range(h)]
for j in range(2):
    board = [i for i in board if i.count('#') != 0]
    board = np.array(board)
    board = board.T
    board = board.tolist()
for i in board:
    print(*i, sep='')

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
grid2 = []
ans = []
cur = 0
for a in grid:
    if "#" in a:
        grid2.append(a)
t_grid2 = np.array(grid2).T.tolist()
for a in t_grid2:
      if "#" in a:
        ans.append(a)
ans = np.array(ans).T.tolist()
for i in range(len(ans)):
  out = "".join(ans[i])
  print(out)

Submission status: Accepted

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

l = []
ll = [[True for i in range(w)] for j in range(h)]
for _ in range(h):
    s = input()
    cells = list(s)
    # print(cells)
    l.append(cells)
    
# print(l)

for h_i in range(h):
    flag = True
    for w_i in range(w):
        # print(l[h_i][w_i])
        if l[h_i][w_i] == "#":
            flag = False
            break
    if flag:
        for w_i in range(w):
            ll[h_i][w_i] = False

for w_i in range(w):
    flag = True
    for h_i in range(h):
        # print(l[h_i][w_i])
        if l[h_i][w_i] == "#":
            flag = False
            break
    if flag:
        for h_i in range(h):
            ll[h_i][w_i] = False
            
# print(ll)

for i in range(h):
    lll = []
    for j in range(w):
        # print(l[i][j])
        if ll[i][j]:
            lll.append(l[i][j])
    if len(lll) > 0:
        print(''.join(lll))

Submission status: Accepted

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

H_idx=[]
for i in range(H):
    count = 0
    for j in range(W):
        if a[i][j]=='.':
            count +=1
    if count != W:
        H_idx.append(i)

W_idx=[]
for i in range(W):
    count = 0
    for j in range(H):
        if a[j][i]=='.':
            count +=1
    if count != H:
        W_idx.append(i)

for i in H_idx:
    ans = ''
    for j in W_idx:
        ans +=a[i][j]
    print(ans)

Submission status: Accepted

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

hh = []
for y in range(h):
    f = True
    for x in range(w):
        if a[y][x] == '#':
            f = False
            break
    if f:
        hh.append(y)

ww = []
for x in range(w):
    f = True
    for y in range(h):
        if a[y][x] == '#':
            f = False
            break
    if f:
        ww.append(x)

ans = []
for y in range(h):
    if y in hh:
        continue
    aa = ''
    for x in range(w):
        if x in ww:
            continue
        aa += a[y][x]
    ans.append(aa)

for aa in ans:
    print(aa)

Submission status: Accepted

Solution code:
import numpy as np

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

A = []
for i in G:
    if not '#' in i:
        continue
    A.append(i)

B = np.array(A)
B = B.T

X = []
for i in B:
    if not '#' in i:
        continue
    X.append(i)

ans = np.array(X)
ans = ans.T

for i in ans:
    print(''.join(i))

Submission status: Accepted

Solution code:
h,w = map(int, raw_input().split())
mat = [list(raw_input()) for _ in range(h)]
rows = set([i for i in range(len(mat)) if '#' in mat[i]])

cols = set([i for i in range(len(mat[0])) if '#' in [mat[j][i] for j in range(len(mat))]])

mat = [mat[i] for i in range(len(mat)) if i in rows]


mat = [[mat[i][j] for j in range(len(mat[0])) if j in cols] for i in range(len(mat))]

for l in mat:
	print ''.join(l)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0
inf = float("inf")

h,w = I()
board = [X() for _ in range(h)]
num = []
nn = []

for i in range(h):
    if "#" not in board[i]:
        num.append(i)

for i in num:
    a = i - count
    del board[a]
    count += 1

for i in range(w):
    nn.append(i)
    for j in range(len(board)):
        if board[j][i] == "#":
            nn.remove(i)
            break

for i in range(len(board)):
    cnt = 0
    for j in nn:
        b = j-cnt
        del board[i][b]
        cnt += 1

for i in range(len(board)):
    print("".join(board[i]))

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().split())
field = [list(input()) for _ in range(h)]

#%%
np_field = np.array(field)
cols = np.logical_not(np.all(np_field=='.', axis=0))
rows = np.logical_not(np.all(np_field=='.', axis=1))

ans = np_field[np.ix_(rows, cols)]

for a in ans:
    print(''.join(map(str, a)))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
L = []
brank = "." * W
for _ in range(H):
  S = input()
  if S == brank:
    continue
  L.append(list(map(str, S)))

if L:
  L1 = list(zip(*L))
  brank = tuple(["." for _ in range(len(L1[0]))])
  ans = []

  for l in L1:
    if l == brank:
      continue
    ans.append(l)


  for a in zip(*ans):
    print("".join(a))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
L = []
brank = "." * W
ban = []
for _ in range(H):
  s = input()
  if brank == s:
    continue
  L.append(list(map(str, s)))
  
for i in range(W):
  for l in L:
    if l[i] != ".":
      break
  else:
    ban.append(i)
    continue

for l in L:
  ans = ""
  for i in range(W):
    if i in ban:
      continue
    ans += l[i]
  print(ans)

Submission status: Accepted

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

H,W = list(map(int,input().split()))
cell = []
for i in range(H):
    s=list(input().rstrip())
    cell.append(s)
h = set(range(H))
w = set(range(W))
sh = []
sw = []
for ih in range(H):
    for iw in range(W):
        if cell[ih][iw] == '#':
            sh.append(ih)
            sw.append(iw)
sh = set(sh)
sw = set(sw)
for iih in sh:
    for iiw in sw:
        print(cell[iih][iiw],end='')
    print('')

Submission status: Accepted

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

a = [list(input()) for i in range(H)]

not_removedH = []#捨てない行
for i in range(H):
    removed = True
    for j in range(W):
        if a[i][j]=="#":#一つでも黒があれば終わり
            removed = False
            break
    if not removed:
        not_removedH.append(i)

not_removedW = []#捨てない列
for j in range(W):
    removed = True
    for i in range(H):
        if a[i][j]=="#":
            removed = False
            break
    if not removed:
        not_removedW.append(j)


#捨てる列を削除していく
for i in range(H):
    tmp = []
    for j in not_removedW:
        tmp.append(a[i][j])
    a[i]=tmp

for i in not_removedH:
    print("".join(a[i]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
a = [list(input()) for i in range(h)]
hl = [0] * h
wl = [0] * w
for i in range(h):
    if '#' in a[i]:
        hl[i] = 1
        for j in range(w):
            if a[i][j] == '#':
                wl[j] = 1

for i in range(h):
    t = []
    for j in range(w):
        if  hl[i] == 1 and wl[j] == 1:
            t.append(a[i][j])
    if len(t) != 0:
        print(''.join(t))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
for i in zip(*[i for i in zip(*[i for i in [input() for i in range(h)] if '#' in i]) if '#' in i]):
  print(''.join(i))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[input() for i in range(h)]

a2=[i for i in a if '#' in i]
a3=[i for i in zip(*a2) if '#' in i]
for i in zip(*a3):
  print(''.join(i))

Submission status: Accepted

Solution code:
h, w, *a = open(0).read().split()
for _ in range(2):
    a = [list(j) for j in zip(*[i for i in a if "#" in i])]
print(*["".join(i) for i in a], sep="\n")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
h, w = map(int, input().split())
a = [[i for i in input()] for _ in range(h)]
a = [i for i in a if "#" in i]
a = [list(i) for i in zip(*a)]
a = [i for i in a if "#" in i]
a = [list(i) for i in zip(*a)]
a = ["".join(i) for i in a]
print(*a, sep="\n")

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

H,W = map(int,readline().split())
a = [readline().rstrip() for i in range(H)]

h = [True] * H
w = [True] * W

for i in range(H):
    if a[i] == "."*W:
        h[i] = False
for i in range(W):
    if all(a[j][i] == "." for j in range(H)):
        w[i] = False
ans = []
for i in range(H):
    s = ""
    for j in range(W):
        if h[i] and w[j]:
            s += a[i][j]
    if s != "":
        print(s)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

H, W = mapint()
query = [list(input()) for _ in range(H)]

rows = [0]*H
cols = [0]*W
for h in range(H):
    for w in range(W):
        if query[h][w]=='#':
            rows[h] = 1
            cols[w] = 1

for h in range(H):
    if not rows[h]:
        continue
    ans = []
    for w in range(W):
        if cols[w]:
            ans.append(query[h][w])
    print(''.join(ans))

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

H,W = I()
maze = [list(s()) for _ in range(H)]
L1 = []
for i in range(H-1,-1,-1):
    if '#' not in maze[i]:
        maze.pop(i)
H = len(maze)
L2 = []
for i in range(W-1,-1,-1):
    Flag = 1
    for j in range(H):
        if maze[j][i] == '#':
            Flag = 0
            break
    if Flag:
        for j in range(H):
            maze[j].pop(i)
for i in range(H):
    print(''.join(maze[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
norow=set()
nocolumn=set()
s=[]
for i in range(h):
  a=input()
  s.append(a)
for i in range(h):
  flag=0
  for j in range(w):
    if s[i][j]=='#':
      flag=1
  if flag==0:
    norow.add(i)
for j in range(w):
  flag=0
  for i in range(h):
    if s[i][j]=='#':
      flag=1
  if flag==0:
    nocolumn.add(j)
for i in range(h):
  if i in norow:
    continue
  ans=[]
  for j in range(w):
    if j in nocolumn:
      continue
    ans.append(s[i][j])
  print(''.join(ans))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

H,W = map(int,readline().split())
a = [readline().rstrip() for i in range(H)]

h = [True] * H
w = [True] * W

for i in range(H):
    if a[i] == "."*W:
        h[i] = False
for i in range(W):
    if all(a[j][i] == "." for j in range(H)):
        w[i] = False
ans = []
for i in range(H):
    s = ""
    for j in range(W):
        if h[i] and w[j]:
            s += a[i][j]
    if s != "":
        ans.append(s)
for s in ans:
    print(s)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
k = []
h_remove = []
w_remove = []
for i in range(h):
    ls = input()
    if ls.count(".") == w:
        h_remove.append(i)
    k.append(ls)
for i in range(w):
    now = ""
    for j in range(h):
        now += k[j][i]
    if now.count(".") == h:
        w_remove.append(i)
h_remove = set(h_remove)
w_remove = set(w_remove)
for i in range(h):
    ans = ""
    if i in  h_remove:
        continue
    else:
        for j in range(w):
            if j in w_remove:
                continue
            else:
                ans += k[i][j]
        print(ans)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
A = [list(input()) for _ in range(H)]
B = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            B[i][j] = 1
C = []
for i in range(H-1,-1,-1):
    if sum(B[i])==0:
        C.append(i)
B1 = []
for i in range(H):
    if i not in C:
        B1.append(B[i])
C = []
for j in range(W):
    flag = 0
    for i in range(len(B1)):
        if B1[i][j]==1:
            flag = 1
            break
    if flag==0:
        C.append(j)
B2 = []
for i in range(len(B1)):
    row = []
    for j in range(W):
        if j not in C:
            row.append(B1[i][j])
    B2.append(row)
B3 = []
for i in range(len(B2)):
    x = ""
    for j in range(len(B2[i])):
        if B2[i][j]==0:
            x += "."
        else:
            x += "#"
    B3.append(x)
for i in range(len(B3)):
    print(B3[i])

Submission status: Accepted

Solution code:
def resolve():
  h, w = map(int, input().split())
  a = []
  for i in range(h):
    s = input()
    if '#' in s:
      a.append(s)
  h = len(a)
  blk_clm = set()  # '#' を含む列
  for i in range(h):
    for j in range(w):
      if a[i][j] == '#':
        blk_clm.add(j)

  for i in range(h):
    for j in range(w):
      if j in blk_clm:
        print(a[i][j], end='')
    print('')

  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
A = []
dame = ["."]*W
for i in range(H):
  temp = str(input())
  temp = list(temp)
  if temp != dame:
    A.append(temp)
#print(A)
S = len(A)
ans = [[] for _ in range(S)]
for j in range(W):
  if all([A[i][j] == "." for i in range(S)]):
    continue
  for t in range(S):
    ans[t].append(A[t][j])
#print(ans)
for i in range(len(ans)):
  temp = "".join(ans[i])
  print(temp)

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

h,w=nii()
a=[list(input()) for i in range(h)]

t_ans=[]
for i in a:
  if '#' in i:
    t_ans.append(i)

t_ans=[i for i in zip(*t_ans)]

ans=[]
for i in t_ans:
  if '#' in i:
    ans.append(i)

ans=[i for i in zip(*ans)]

for i in ans:
  print(''.join(i))

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
A=[list(input()) for i in range(H)]
a=0
for i in range(H):
  if set(A[i-a])==set(['.']):
    del A[i-a]
    a+=1
b=0
for i in range(W):
  for j in range(H-a):
    if A[j][i-b]=='#':
      break
    if j==H-a-1:
      for k in range(H-a):
        del A[k][i-b]
      b+=1
for i in range(len(A)):
  print(''.join(A[i]))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
A = [input() for i in range(H)]
B = []
for row in A:
    if all(c=='.' for c in row): continue
    B.append(row)

ans = ['' for _ in range(len(B))]
for col in zip(*B):
    if all(c=='.' for c in col): continue
    for i,c in enumerate(col):
        ans[i] += c

for row in ans:
    print(row)

Submission status: Accepted

Solution code:
import sys
import numpy as np

rm = lambda: map(int, sys.stdin.readline().split())
rr = lambda: sys.stdin.readline().rstrip()

h, w = rm()
li = np.array([np.array(list(rr())) for _ in range(h)])
li2 = []
li3 = []
for i in li:
  if '#' in i:
    li2.append(i)
li2 = np.array(li2).T
for i in li2:
  if '#' in i:
    li3.append(i)
li3 = np.array(li3).T
for i in li3:
  print(''.join(i))

Submission status: Accepted

Solution code:
def solve():
    H, W = map(int, input().split())
    row = [False for _ in range(H)]
    column = [False for _ in range(W)]
    a = [['' for _ in range(W)] for _ in range(H)]
    for i in range(H):
        a[i] = input()
        for j in range(W):
            if a[i][j] == '#':
                row[i] = True
                column[j] = True

    for i in range(H):
        if row[i]:
            for j in range(W):
                if column[j]:
                    print(a[i][j], end='')
            print()


if __name__ == "__main__":
    solve()