Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq
from collections import Counter

H,W,M = map(int, input().split())
BH = [[0,i] for i in range(H)]
BW = [[0,i] for i in range(W)]
Bomb = set()
for _ in range(M):
  h,w = map(int, input().split())
  h,w = h-1,w-1
  BH[h][0] += 1
  BW[w][0] += 1
  Bomb.add((h,w))
  
BH.sort(reverse=True)
BW.sort(reverse=True)
  
"""
print(BombC)
print(BH)
print(BW)
"""

h = []
ans = 0
nowh, noww = 0,0
i,j = 0,0
BHMAX, BWMAX = BH[i][0], BW[j][0]
heapq.heappush(h, (-BH[i][0] - BW[j][0], BH[i][1], BW[j][1], i,j))
while h:
  cost, nowh, noww,i,j = heapq.heappop(h)
  cost = -cost
  #print(cost,nowh,noww)
  if (nowh,noww) not in Bomb:
    ans = max(ans,cost)
    break
  else:
    ans = max(ans, cost-1)
    if i+1 < H and BH[i+1][0] == BHMAX:
      heapq.heappush(h, (-BH[i+1][0] - BW[j][0], BH[i+1][1], BW[j][1],i+1, j))
    if j+1 < W and BW[i+1][0] == BWMAX:
      heapq.heappush(h, (-BH[i][0] - BW[j+1][0], BH[i][1], BW[j+1][1],i, j+1))
      
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

H,W,M = map(int, input().split())
BH = [0 for i in range(H)]
BW = [0 for i in range(W)]
Bomb = set()
for _ in range(M):
  h,w = map(int, input().split())
  h,w = h-1,w-1
  BH[h] += 1
  BW[w] += 1
  Bomb.add((h,w))
  
BHMAX = max(BH)
BWMAX = max(BW)
HS = set()
WS = set()
for i in range(H):
  if BH[i] == BHMAX:
    HS.add(i)
for i in range(W):
  if BW[i] == BWMAX:
    WS.add(i)
    
ans = 0
cnt = 0
for h,w in Bomb:
  if h in HS and w in WS:
    ans = max(ans, BHMAX + BWMAX - 1)
    cnt += 1

if cnt < len(HS) * len(WS):
  ans = BHMAX + BWMAX
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq
from collections import Counter

H,W,M = map(int, input().split())
BH = [[0,i] for i in range(H)]
BW = [[0,i] for i in range(W)]
Bomb = set()
for _ in range(M):
  h,w = map(int, input().split())
  h,w = h-1,w-1
  BH[h][0] += 1
  BW[w][0] += 1
  Bomb.add((h,w))
  
BH.sort(reverse=True)
BW.sort(reverse=True)
  
"""
print(BombC)
print(BH)
print(BW)
"""

h = []
ans = 0
nowh, noww = 0,0
i,j = 0,0
heapq.heappush(h, (-BH[i][0] - BW[j][0], BH[i][1], BW[j][1], i,j))
while h:
  cost, nowh, noww,i,j = heapq.heappop(h)
  cost = -cost
  #print(cost,nowh,noww)
  if (nowh,noww) not in Bomb:
    ans = max(ans,cost)
    break
  else:
    ans = max(ans, cost-1)
    if i+1 < H:
      heapq.heappush(h, (-BH[i+1][0] - BW[j][0], BH[i+1][1], BW[j][1],i+1, j))
    if j+1 < W:
      heapq.heappush(h, (-BH[i][0] - BW[j+1][0], BH[i][1], BW[j+1][1],i, j+1))
      
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

H,W,M = map(int, input().split())
BH = [0 for i in range(H)]
BW = [0 for i in range(W)]
Bomb = set()
for _ in range(M):
  h,w = map(int, input().split())
  h,w = h-1,w-1
  BH[h] += 1
  BW[w] += 1
  Bomb.add((h,w))
  
BHMAX = max(BH)
BWMAX = max(BW)
HS = set()
WS = set()
for i in range(H):
  if BH[i] == BHMAX:
    HS.add(i)
for i in range(W):
  if BW[i] == BWMAX:
    WS.add(i)
    
ans = 0
cnt = 0
for h,w in Bomb:
  if h in HS and w in WS:
    ans = max(ans, BHMAX + BWMAX - 1)
    cnt += 1

if cnt < len(HS) * len(WS):
  ans = BHMAX + BWMAX
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq
from collections import Counter

H,W,M = map(int, input().split())
BH = [[0,i] for i in range(H)]
BW = [[0,i] for i in range(W)]
Bomb = []
for _ in range(M):
  h,w = map(int, input().split())
  h,w = h-1,w-1
  BH[h][0] += 1
  BW[w][0] += 1
  Bomb.append((h,w))
  
BH.sort(reverse=True)
BW.sort(reverse=True)
  
BombC = Counter(Bomb)
"""
print(BombC)
print(BH)
print(BW)
"""

h = []
ans = 0
nowh, noww = 0,0
i,j = 0,0
heapq.heappush(h, (-BH[i][0] - BW[j][0], BH[i][1], BW[j][1], i,j))
while h:
  cost, nowh, noww,i,j = heapq.heappop(h)
  cost = -cost
  #print(cost,nowh,noww)
  if (nowh,noww) not in BombC:
    ans = max(ans,cost)
    break
  else:
    ans = max(ans, cost-BombC[(nowh,noww)])
    if i+1 < H:
      heapq.heappush(h, (-BH[i+1][0] - BW[j][0], BH[i+1][1], BW[j][1],i+1, j))
    if j+1 < W:
      heapq.heappush(h, (-BH[i][0] - BW[j+1][0], BH[i][1], BW[j+1][1],i, j+1))
      
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

H,W,M = map(int, input().split())
BH = [0 for i in range(H)]
BW = [0 for i in range(W)]
Bomb = set()
for _ in range(M):
  h,w = map(int, input().split())
  h,w = h-1,w-1
  BH[h] += 1
  BW[w] += 1
  Bomb.add((h,w))
  
BHMAX = max(BH)
BWMAX = max(BW)
HS = set()
WS = set()
for i in range(H):
  if BH[i] == BHMAX:
    HS.add(i)
for i in range(W):
  if BW[i] == BWMAX:
    WS.add(i)
    
ans = 0
cnt = 0
for h,w in Bomb:
  if h in HS and w in WS:
    ans = max(ans, BHMAX + BWMAX - 1)
    cnt += 1

if cnt < len(HS) * len(WS):
  ans = BHMAX + BWMAX
print(ans)