Problem:
 Score : 500 points    Problem Statement We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i -th target is \left(h_i, w_i \right) .  Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.  Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.      Constraints  All values in input are integers.  1 \leq H, W \leq 3 \times 10^5  1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)  1 \leq h_i \leq H  1 \leq w_i \leq W  \left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)         Input Input is given from Standard Input in the following format:  H  W  M  h_1  w_1  \vdots  h_M  w_M       Output Print the answer.        Sample Input 1 2 3 3
2 2
1 1
1 3      Sample Output 1 3  We can destroy all the targets by placing the bomb at \left(1, 2\right) .       Sample Input 2 3 3 4
3 3
3 1
1 1
1 2      Sample Output 2 3       Sample Input 3 5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4      Sample Output 3 6

Solution (Python):
H, W, M = map(int,input().split())
P = [list(map(int, input().split())) for i in range(M)]
for p in P:
    p[0] -= 1
    p[1] -= 1
cnth = [0]*H
cntw = [0]*W
ans = 0
for p in P:
    cnth[p[0]] += 1
for p in P:
    cntw[p[1]] += 1
mxh = max(cnth)
mxw = max(cntw)
cntx = 0
for p in P:
    if cnth[p[0]] == mxh and cntw[p[1]] == mxw:
        cntx += 1
if cntx == cnth.count(mxh) * cntw.count(mxw):
    ans = mxh + mxw -1
else:
    ans = mxh + mxw
print(ans)

Solution (Python):
H, W, M = map(int,input().split())
P = [list(map(int, input().split())) for i in range(M)]
for p in P:
    p[0] -= 1
    p[1] -= 1
cnth = [0]*H
cntw = [0]*W
ans = 0
for p in P:
    cnth[p[0]] += 1
for p in P:
    cntw[p[1]] += 1
mxh = max(cnth)
mxw = max(cntw)
cntx = 0
for p in P:
    if cnth[p[0]] == mxh and cntw[p[1]] == mxw:
        cntx += 1
if cntx == cnth.count(mxh) * cntw.count(mxw):
    ans = mxh + mxw -1
else:
    ans = mxh + mxw
print(ans)

Solution (Python):

h, w, m = map(int, input().split())

h_b_list = [0 for i in range(h)]
w_b_list = [0 for i in range(w)]

targets = set()
for i in range(m):
  bh, bw = map(int, input().split())
  targets.add((bh, bw))
  h_b_list[bh-1] += 1
  w_b_list[bw-1] += 1
  
h_max = max(h_b_list)
w_max = max(w_b_list)

h_cans = []
w_cans = []
for i in range(h):
  if h_b_list[i] == h_max:
    h_cans.append(i)
    
for i in range(w):
  if w_b_list[i] == w_max:
    w_cans.append(i)

for hi in h_cans:
  for wi in w_cans:
    if (hi+1, wi+1) not in targets:
      print(h_max+w_max)
      exit()
else:
  print(h_max+w_max-1)

Solution (Python):
def main():
    H,W,M = map(int,input().split())
    row = [0 for i in range(H)]
    col = [0 for i in range(W)]
    target  = set()

    for _ in range(M):
        h,w = map(lambda x: int(x)-1,input().split())
        row[h] += 1
        col[w] += 1
        target.add((h,w))
    max_r = max(row)
    max_w = max(col)

    candidate_r = []
    for i,r in enumerate(row):
        if r == max_r:
            candidate_r.append(i)
    candidate_c = []
    for i,c in enumerate(col):
        if c == max_w:
            candidate_c.append(i)

    for idx_r in candidate_r:
        for idx_c in candidate_c:
            if (idx_r,idx_c) not in target:
                print(max_r+max_w)
                return
    print(max_r+max_w-1)
    return

if __name__ == '__main__':
    main()

Solution (Python):
H,W,M=map(int,input().split())
l = [ list(map(int,input().split(" "))) for i in range(M)]
h=[0]*H
w=[0]*W
for i in range(M):
    h[l[i][0]-1]+=1
    w[l[i][1]-1]+=1
maxh=max(h)
maxw=max(w)
hlist=[]
wlist=[]
for i in range(H):
    if h[i]==maxh:
        hlist.append(i)
for i in range(W):
    if w[i]==maxw:
        wlist.append(i)
c=0
for i in range(M):
  if h[l[i][0]-1]==maxh and w[l[i][1]-1]==maxw:
    c+=1
if len(hlist)*len(wlist)==c:
  print(maxh+maxw-1)
else:
  print(maxh+maxw)

Solution (Python):
H,W,M=map(int,input().split())
row=[0]*H
col=[0]*W
target=set()
for i in range(M):
    h,w=map(int,input().split())
    row[h-1]+=1
    col[w-1]+=1
    target.add((h-1,w-1))
A=max(row)
B=max(col)
a=[x for x in range(H) if row[x]==A]
b=[x for x in range(W) if col[x]==B]
for i in a:
    for j in b:
        if (i,j) not in target:
            print(A+B)
            exit(0)
print(A+B-1)

Solution (Python):
H,W,M=map(int,input().split())
row=[0]*H
col=[0]*W
target=set()
for i in range(M):
    h,w=map(int,input().split())
    row[h-1]+=1
    col[w-1]+=1
    target.add((h-1,w-1))
A=max(row)
B=max(col)
a=[x for x in range(H) if row[x]==A]
b=[x for x in range(W) if col[x]==B]
cnt=0
for i in a:
    for j in b:
        cnt+=1
        if (i,j) not in target:
            print(A+B)
            exit(0)
        if cnt>M:
            print(A+B-1)
            exit(0)
print(A+B-1)

Solution (Python):


from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)


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

num_bombs_each_row = [0] * H
num_bombs_each_col = [0] * W
bombs = set()

for _ in range(M):
    h, w = map(int, input().split())
    
    h -= 1
    w -= 1

    bombs.add((h, w))
    num_bombs_each_row[h] += 1
    num_bombs_each_col[w] += 1

max_bombs_in_row = max(num_bombs_each_row)
max_bombs_in_col = max(num_bombs_each_col)

rows_with_max_bombs = []
for row in range(H):
    if num_bombs_each_row[row] == max_bombs_in_row:
        rows_with_max_bombs.append(row)

cols_with_max_bombs = []
for col in range(W):
    if num_bombs_each_col[col] == max_bombs_in_col:
        cols_with_max_bombs.append(col)

found = False
for row in rows_with_max_bombs:
    for col in cols_with_max_bombs:
        if (row, col) not in bombs:
            found = True
            break

ans = max_bombs_in_row + max_bombs_in_col
if found:
    print(ans)
else:
    print(ans - 1)

Solution (Python):


from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)


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

num_bombs_each_row = [0] * H
num_bombs_each_col = [0] * W
bombs = set()

for _ in range(M):
    h, w = map(int, input().split())
    
    h -= 1
    w -= 1

    bombs.add((h, w))
    num_bombs_each_row[h] += 1
    num_bombs_each_col[w] += 1

max_bombs_in_row = max(num_bombs_each_row)
max_bombs_in_col = max(num_bombs_each_col)

rows_with_max_bombs = []
for row in range(H):
    if num_bombs_each_row[row] == max_bombs_in_row:
        rows_with_max_bombs.append(row)

cols_with_max_bombs = []
for col in range(W):
    if num_bombs_each_col[col] == max_bombs_in_col:
        cols_with_max_bombs.append(col)

found = False
for row in rows_with_max_bombs:
    for col in cols_with_max_bombs:
        if not (row, col) in bombs:
            found = True
            break

ans = max_bombs_in_row + max_bombs_in_col
if found:
    print(ans)
else:
    print(ans - 1)

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

countW = [0]*(W+1)
countH = [0]*(H+1)
bomb = []
for i in range(M):
    h, w = map(int, input().split())
    bomb.append((h, w))
    countH[h] += 1
    countW[w] += 1

maxW = max(countW)
maxH = max(countH)
ans = maxW + maxH - 1
countMaxW = countW.count(maxW)
countMaxH = countH.count(maxH)

sumz = 0
for i in range(M):
    h, w = bomb[i]
    if countH[h] == maxH and countW[w] == maxW:
        sumz += 1

if countMaxW*countMaxH > sumz:
    ans += 1
print(ans)

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

bombs = set()

hc = [0 for h in range(H)]
wc = [0 for w in range(W)]

for _ in range(M):
    h,w = map(int, input().split())
    h-=1
    w-=1
    bombs.add((h, w))
    hc[h] += 1
    wc[w] += 1

hmax = max(hc)
wmax = max(wc)

htable = []
for h in range(H):
    if hc[h] == hmax:
        htable.append(h)

wtable = []
for w in range(W):
    if wc[w] == wmax:
        wtable.append(w)






found = False
for h in htable:
    for w in wtable:
        if (h,w) not in bombs:
            print(hmax + wmax)
            exit(0)

print(hmax + wmax - 1)

Solution (Python):
import sys

H, W, M = map(int, input().split())
A = [list(map(lambda x: int(x) - 1, input().split())) for i in range(M)]

cnth = [0] * H
cntw = [0] * W
st = set()

for y, x in A:
    cnth[y] += 1
    cntw[x] += 1
    st.add((y, x))

my = max(cnth)
mx = max(cntw)

Y = []
X = []

for i in range(H):
    if cnth[i] == my:
        Y.append(i)

for i in range(W):
    if cntw[i] == mx:
        X.append(i)

for y in Y:
    for x in X:
        if not (y, x) in st:
            print(my + mx)
            exit(0)

print(my + mx - 1)

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

r_n = [0] * h
c_n = [0] * w
bomb_table = set()
for i in range(k):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    r_n[hi] += 1
    c_n[wi] += 1
    bomb_table.add((hi, wi))

max_x = max(c_n)
max_y = max(r_n)
ans = max_x + max_y

max_x_table = set()
max_y_table = set()

for i in range(h):
    if r_n[i] == max_y:
        max_y_table.add(i)

for i in range(w):
    if c_n[i] == max_x:
        max_x_table.add(i)

for i in max_y_table:
    for j in max_x_table:
        if (i, j) not in bomb_table:
            print(ans)
            exit(0)
print(ans - 1)

Solution (Python):
import collections

h,w,m = map(int,input().split())

gyou = [0 for i in range(h)]
retu = [0 for i in range(w)]
l = []

for i in range(m):
    y,x = map(int,input().split())
    gyou[y-1] += 1
    retu[x-1] += 1
    l.append((y-1,x-1))

ymax = max(gyou)
xmax = max(retu)
c_ymax = gyou.count(ymax)
c_xmax = retu.count(xmax)
ans = ymax + xmax

c = 0
for y,x in l:
    if gyou[y] == ymax and retu[x] == xmax:
        c += 1

if c == c_ymax*c_xmax:
    print(ans-1)
else:
    print(ans)

Solution (Python):
from collections import deque,defaultdict
import heapq as hp
def solve():
    h,w,m=map(int,input().strip().split(" "))
    dr=defaultdict(int)
    dc=defaultdict(int)
    s=set()
    for _ in range(m):
        a,b=map(int,input().strip().split(" "))
        dr[a]+=1
        dc[b]+=1
        s.add((a,b))

    kr=max(dr.values())
    kc=max(dc.values())
    rs=[]
    cs=[]
    rs+=[i for i in dr.keys() if dr[i]==kr ]
    cs+=[i for i in dc.keys() if dc[i]==kc ]
    for i in rs:
        for j in cs:
            if (i,j) not in s:
                print(kr+kc)
                exit()

    print(kr+kc-1)






solve()

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

visited = set()

h_sum = [0] * H
w_sum = [0] * W

for i in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    visited.add((h, w))
    h_sum[h] += 1
    w_sum[w] += 1

max_h = max_w = 0
can_h = []
can_w = []
for i in range(H):
    max_h = max(max_h, h_sum[i])
    
for i in range(W):
    max_w = max(max_w, w_sum[i])

for i in range(H):
    if h_sum[i] == max_h:
        can_h.append(i)
for i in range(W):
    if w_sum[i] == max_w:
        can_w.append(i)
        
ans = max_h + max_w

for i in can_h:
    for j in can_w:
        if (i, j) not in visited:
            print(ans)
            exit()

print(ans-1)

Solution (Python):
import sys
input = sys.stdin.readline

h, w, m = map(int,input().split())

row = [0]*h
col = [0]*w
enemy = []

for i in range(m):
    hh, ww = map(lambda x:int(x)-1,input().split())
    row[hh] += 1
    col[ww] += 1
    enemy.append((hh,ww))

enemy = set(enemy)
rmax = max(row)
cmax = max(col)

on_r = row.count(rmax)
on_c = col.count(cmax)

on = 0
for hh, ww in enemy:
    if row[hh] == rmax and col[ww] == cmax:
        on += 1
if on == on_r*on_c:
    print(rmax+cmax-1)
else:
    print(rmax+cmax)

Solution (Python):
import sys
import numpy as np

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def main():
    h, w, m = MII()
    bombs = [MIIZ() for _ in range(m)]
    a = [0]*h
    b = [0]*w
    for y,x in bombs:
        a[y] += 1
        b[x] += 1
    maxa = max(a)
    maxb = max(b)
    sumv = maxa + maxb
    c = a.count(maxa)*b.count(maxb) - sum(a[y]+b[x] == sumv for y,x in bombs)
    print(sumv - (c<=0))

if __name__ == '__main__':
    main()

Solution (Python):
H, W, M = map(int,input().split())
row = [0]*H
col = [0]*W
target = set()
for i in range(M):
    h,w = map(int,input().split())
    row[h-1] += 1
    col[w-1] += 1
    target.add((h-1,w-1))
ans = -1
for h,w in target:
    ans = max(ans, row[h]+col[w]-1)
A = max(row)
B = max(col)
a = [x for x in range(H) if row[x] == A]
b = [x for x in range(W) if col[x] == B]
cnt = 0
for i in a:
    for j in b:
        cnt += 1
        if (i,j) not in target:
            print(A+B)
            exit(0)
        if cnt > M:
            print(A+B-1)
            exit(0)
print(A+B-1)

Solution (Python):
h,w,m=map(int,input().split())
hs=[0 for _ in range(h)]
ws=[0 for _ in range(w)]
B=set()
for _ in range(m):
  a,b=map(int,input().split())
  a-=1
  hs[a]+=1
  b-=1
  ws[b]+=1
  B.add((a,b))
hmax=-1
wmax=-1
for i in range(h):
  hmax=max(hmax,hs[i])
for j in range(w):
  wmax=max(wmax,ws[j])
mh=[]
mw=[]
for i in range(h):
  if hs[i]==hmax:
    mh.append(i)
for j in range(w):
  if ws[j]==wmax:
    mw.append(j)

ans=hmax+wmax
cnt=0
for i in mh:
  for j in mw:
    if (i,j) in B:
      continue
    print(ans)
    exit()
print(ans-1)

Solution (Python):
from collections import defaultdict

H, W, M = list(map(int, input().split()))
hw = [list(map(int, input().split())) for _ in range(M)]

d1 = defaultdict(int)
d2 = defaultdict(int)

b = defaultdict(int)

for h, w in hw:
    d1[h] += 1
    d2[w] += 1
    b[(h, w)] = 1

m1 = max(d1.values())
m2 = max(d2.values())

e1 = [k for k in d1.keys() if d1[k] == m1]
e2 = [k for k in d2.keys() if d2[k] == m2]

flag = True

for x in e1:
    for y in e2:
        if b[(x, y)] == 1:
            pass
        else:
            flag = False
            break
    if not flag:
        break

if flag:
    print(m1+m2-1)
else:
    print(m1+m2)

Solution (Python):
import sys
import itertools


def resolve(in_):
    H, W, M = map(int, next(in_).split())
    targets = tuple(tuple(map(int, line.split())) for line in in_)
    
    target_h = {}
    target_w = {}
    target_hw = set()
    for h, w in targets:
        target_h[h] = target_h.setdefault(h, 0) + 1
        target_w[w] = target_w.setdefault(w, 0) + 1
        target_hw.add((h, w))

    max_h = max(target_h.values())
    max_w = max(target_w.values())

    h_set = {h for h, v in target_h.items() if max_h == v}
    w_set = {w for w, v in target_w.items() if max_w == v}

    ans = max_h + max_w - 1
    for hw in itertools.product(h_set, w_set):
        if hw not in target_hw:
            ans += 1
            break
    
    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import itertools


def resolve(in_):
    H, W, M = map(int, next(in_).split())
    targets = tuple(tuple(map(int, line.split())) for line in in_)
    
    target_h = {}
    target_w = {}
    target_hw = set()
    for h, w in targets:
        target_h[h] = target_h.setdefault(h, 0) + 1
        target_w[w] = target_w.setdefault(w, 0) + 1
        target_hw.add((h, w))

    max_h = max(target_h.values())
    max_w = max(target_w.values())

    h_set = {h for h, v in target_h.items() if max_h == v}
    w_set = {w for w, v in target_w.items() if max_w == v}

    ans = max_h + max_w - 1
    for hw in itertools.product(h_set, w_set):
        if hw not in target_hw:
            ans += 1
            break
    
    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
h,w,m = map(int,input().split())
dh = defaultdict(int)
dw = defaultdict(int)
l=defaultdict(tuple)
for _ in range(m):
    x,y = map(int,input().split())
    dh[x]+=1
    dw[y]+=1
    l[(x,y)]=1
m = 0
for i in range(1,h+1):
    if dh[i]>m:
        m = dh[i]
        hi=i
c=m
m = 0
for i in range(1,w+1):
    d = dw[i]
    if l[(hi,i)]==1:
        d-=1
    m = max(m,d)
c+=m
m = 0
wi=0
for i in range(1,w+1):
    if dw[i]>m:
        m = dw[i]
        wi=i
c2=m
m = 0
for i in range(1,h+1):
    d = dh[i]
    if l[(i,wi)]==1:
        d-=1
    m = max(m,d)
c2+=m
print(max(c,c2))

Solution (Python):
h,w,m=map(int,input().split())
hl=[0]*(h)
wl=[0]*(w)
bom=[]
for i in range(m):
  h1,w1=map(int,input().split())
  bom.append([h1-1,w1-1])
  hl[h1-1]+=1
  wl[w1-1]+=1

mh=max(hl)
mw=max(wl)
ans=mh+mw
mhl=set()
mwl=set()
for i in range(h):
  if mh==hl[i]:
    mhl.add(i)
    
for i in range(w):
  if mw==wl[i]:
    mwl.add(i)

count=len(mhl)*len(mwl)
for i in range(len(bom)):
  if bom[i][0] in mhl and bom[i][1] in mwl:
    count-=1
if count>0:
  print(ans)
else:
  print(ans-1)

Solution (Python):
h,w,m=map(int,input().split())
d_h=[0]*(h+1)
d_w=[0]*(w+1)
d=[]
for i in range(m):
  nh,nw=map(int,input().split())
  d_h[nh]+=1
  d_w[nw]+=1
  d.append([nh,nw])
  



hantei=0
ans1=max(d_h)
memoh=set()
for i in range(1,h+1):
  hantei=d_h[i]
  if ans1==hantei:
    memoh.add(i)
    
hantei=0
ans2=max(d_w)
memow=set()
for j in range(1,w+1):
  hantei=d_w[j]
  if ans2 == hantei:
    memow.add(j)
ans = ans2+ans1-1

count = len(memoh)*len(memow)
for hw in d:
  if hw[0] in memoh and hw[1] in memow:
    count-=1
if count>0:
  print(ans+1)
else:
  print(ans)

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

    row = [0] * (H + 1)
    row_set = [set() for _ in range(H+1)]
    column = [0] * (W + 1)
    column_set = [set() for _ in range(W+1)]
    
    ms = []
    positions = set()
    for m in range(M):
        h, w = map(int, input().split())

        positions.add((h, w))
        
        row[h] += 1
        row_set[h].add(w)

        column[w] += 1
        column_set[w].add(h)



    max_rows = set()
    maxR = -1
    for i, v in enumerate(row[1:]):
        if v > maxR:
            max_rows = set()
            max_rows.add(i+1)
            maxR = v
        elif v == maxR:
            max_rows.add(i+1)


    max_cols = set()
    maxC = -1
    for i, v in enumerate(column[1:]):
        if v > maxC:
            max_cols = set()
            max_cols.add(i+1)
            maxC = v
        elif v == maxC:
            max_cols.add(i + 1)


    for y in max_rows:
        for x in max_cols:
            if not (y, x) in positions:
                print(maxR + maxC)
                exit()
    print(maxR + maxC - 1)
main()

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

h_list = [0 for i in range(H)]
w_list = [0 for i in range(W)]

point = {}
for _ in range(M):
    h, w = map(int, input().split())
    h_list[h - 1] += 1
    w_list[w - 1] += 1
    point[(h - 1, w - 1)] = 1

hmax = 0
wmax = 0
hmap = {}
wmap = {}
for i, h in enumerate(h_list):
    if hmax <= h:
        hmax = h
        hmap.setdefault(h, []).append(i)
for i, w in enumerate(w_list):
    if wmax <= w:
        wmax = w
        wmap.setdefault(w, []).append(i)




h_index = h_list.index(hmax)
w_index = w_list.index(wmax)

for h in hmap[hmax]:
    for w in wmap[wmax]:
        if (h, w) not in point:
            print(hmax + wmax)
            exit(0)

print(hmax + wmax - 1)

Solution (Python):
h,w,n=map(int,input().split())
H=[0]*h
W=[0]*w
L=[(0,0)]*n
for i in range(n):
    a,b=map(int,input().split())
    L[i]=(a,b)
    H[a-1]+=1
    W[b-1]+=1
h_max=max(H)
w_max=max(W)
ans=h_max+w_max
cnt=0
for l in L:
    if H[l[0]-1]==h_max and W[l[1]-1]==w_max:
        cnt+=1
if H.count(h_max)*W.count(w_max)==cnt:
    ans-=1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

h, w, m = map(int, input().split())


hs = [0] * h
ws = [0] * w

s = set()

readline = sys.stdin.readline
for _ in range(m):
    r, c = [int(i) for i in readline().split()]
    r -= 1
    c -= 1
    hs[r] += 1
    ws[c] += 1
    s.add((r, c))


mh = 0
mw = 0
for i in range(h):
    mh = max(mh, hs[i])
for j in range(w):
    mw = max(mw, ws[j])


si = []
sj = []
for i in range(h):
    if mh == hs[i]:
        si.append(i)
for j in range(w):
    if mw == ws[j]:
        sj.append(j)


ans = mh + mw
for i in si:
    for j in sj:
        if (i, j) in s:
            continue
        print(ans)
        exit()
print(ans-1)

Solution (Python):
import numpy as np
H, W, M = map(int, input().split())
col = np.zeros(H)
row = np.zeros(W)
memo = []
for i in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    col[h] += 1
    row[w] += 1
    memo.append((h,w))

col_max = col.max()
row_max = row.max()

col_max_indexes = np.where(col == col_max)[0]
row_max_indexes = np.where(row == row_max)[0]

ans = col_max + row_max - 1
memo = set(memo)
for c in col_max_indexes:
    for r in row_max_indexes:
        if (c,r) not in memo:
            ans = col_max + row_max
            print(int(ans))
            exit()

print(int(ans))

Solution (Python):
import sys
import  math
import fractions
from collections import defaultdict
import heapq
from bisect import bisect_left,bisect

stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

H,W,M=nm()
r=[0]*(10**6)
c=[0]*(10**6)
mass=set()
for i in range(M):
    h,w=nm()
    mass.add(H*h+w)
    r[h]+=1
    c[w]+=1

rl=[]
cl=[]

r_max=0
for i in range(1,H+1):
    r_max=max(r[i],r_max)
    
c_max=0
for i in range(1,W+1):
    c_max=max(c[i],c_max)

for i in range(1,H+1):
    if(r[i]==r_max):
        rl.append(i)

for i in range(1,W+1):
    if(c[i]==c_max):
        cl.append(i)



for i in rl:
    for j in cl:
        if(i*H+j not in mass):
            print(r_max+c_max)
            sys.exit(0)


print(r_max+c_max-1)

Solution (Python):
from collections import defaultdict
import heapq

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

col_cnt = [0] * W
row_cnt = [0] * H
col_max = 0
row_max = 0

w2h = defaultdict(list)
h2w = defaultdict(list)
bombs = set()

for _ in range(M):
    h, w = map(lambda x:int(x)-1, input().split())
    row_cnt[h] += 1
    col_cnt[w] += 1
    row_max = max(row_max, row_cnt[h])
    col_max = max(col_max, col_cnt[w])
    bombs.add((h, w))

max_rows = set()
max_cols = set()

for h in range(H):
    if row_cnt[h] == row_max:
        max_rows.add(h)
for w in range(W):
    if col_cnt[w] == col_max:
        max_cols.add(w)

cnt = 0
for h, w in bombs:
    if h in max_rows and w in max_cols:
        cnt += 1

if cnt == len(max_rows) * len(max_cols):
    print(row_max + col_max-1)
else:
    print(row_max + col_max)

Solution (Python):
H, W, M = map(int, input().split())
 
hs = [0] * H
ws = [0] * W
boms = set()
 
for i in range(M):
    r,c = map(int, input().split())
    r -= 1
    c -= 1
 
    hs[r] += 1
    ws[c] += 1
    boms.add((r, c))
mh, mw = 0, 0
for i in range(H):
    mh = max(mh, hs[i])
for j in range(W):
    mw = max(mw, ws[j])
max_h_list = []
max_w_list = []
    
for i in range(H):
    if hs[i] == mh: max_h_list.append(i)    
for j in range(W):
    if ws[j] == mw: max_w_list.append(j)
ans = mh + mw
 
for i in max_h_list:
    for j in max_w_list:
        if (i,j) not in boms:
            print(ans)
            exit()
print(ans-1)

Solution (Python):
H, W, M = map(int, input().split())
 
h = [0] * H
w = [0] * W
Mem = [tuple(map(int, input().split())) for _ in range(M) ]
for i, j in Mem:
  h[i-1] += 1
  w[j-1] += 1

maxh = max(h)
maxw = max(w)
 
listh = {i for i, v in enumerate(h, 1) if v == maxh}
listw = {j for j, v in enumerate(w, 1) if v == maxw}

cnt = len(listh) * len(listw)

for i, j in Mem:
  if i in listh and j in listw:
    cnt -= 1
 
if cnt:
  ans = maxh + maxw
else:
  ans = maxh + maxw - 1

print(ans)

Solution (Python):
h,w,m=map(int,input().split())
item=[list(map(int,input().split())) for i in range(m)]
row=[0]*h
col=[0]*w
for i in range(m):
    x,y=item[i]
    row[x-1]+=1
    col[y-1]+=1
mr,mc=max(row),max(col)
xr=set([i for i in range(h) if row[i]==mr])
xc=set([i for i in range(w) if col[i]==mc])
check=len(xr)*len(xc)
for i in range(m):
    r,c=item[i]
    if r-1 in xr and c-1 in xc:
        check-=1
print(mr+mc if check>0 else mr+mc-1)

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

hs = [0] * H
ws = [0] * W
checked = set()

for i in range(M):
    r,c = map(int, input().split())
    r -= 1
    c -= 1

    hs[r] += 1
    ws[c] += 1
    checked.add((r, c))

mh, mw = 0, 0
for i in range(H):
    mh = max(mh, hs[i])
for j in range(W):
    mw = max(mw, ws[j])

max_h_list = []
max_w_list = []
    
for i in range(H):
    if hs[i] == mh: max_h_list.append(i)    
for j in range(W):
    if ws[j] == mw: max_w_list.append(j)

ans = mh + mw

for i in max_h_list:
    for j in max_w_list:
        if (i,j) not in checked:
            print(ans)
            exit()
print(ans-1)

Solution (Python):
from collections import defaultdict


def main():
    height, width, target_count = [int(x) for x in input().split()]
    count_by_height = defaultdict(int)
    count_by_width = defaultdict(int)
    bomb_locations = set()
    for _ in range(target_count):
        h, w = [int(x) - 1 for x in input().split()]
        count_by_height[h] += 1
        count_by_width[w] += 1
        bomb_locations.add((h, w))
    max_h = max(v for v in count_by_height.values())
    max_w = max(v for v in count_by_width.values())
    max_h_rows = [i for i, x in count_by_height.items() if x == max_h]
    max_w_columns = [i for i, x in count_by_width.items() if x == max_w]
    all_crossing_bomb = all((h, w) in bomb_locations
                            for h in max_h_rows for w in max_w_columns)
    return max_h + max_w - all_crossing_bomb


if __name__ == '__main__':
    print(main())

Solution (Python):
from bisect import bisect_left, bisect_right

H, W, M = map(int,input().split())
targets = [list(map(lambda x: int(x) - 1, input().split())) for i in range(M)]

cnth = [0] * H
cntw = [0] * W
wlist = [[] for i in range(H)]

for i in range(M):
	hi, wi = targets[i]
	cnth[hi] += 1
	cntw[wi] += 1
	wlist[hi].append(wi)

maxhi = []
maxwi = []
maxh = max(cnth)
maxw = max(cntw)

for i in range(H):
	wlist[i].sort()
	if cnth[i] == maxh:
		maxhi.append(i)
for i in range(W):
	if cntw[i] == maxw:
		maxwi.append(i)

for i in range(len(maxhi)):
	for j in range(len(maxwi)):
		if bisect_right(wlist[maxhi[i]], maxwi[j]) - bisect_left(wlist[maxhi[i]], maxwi[j]) == 0:
			print(maxh + maxw)
			exit()
print(maxh + maxw - 1)

Solution (Python):
from bisect import bisect_left, bisect_right

def exists(ite, x):
	return bool(bisect_right(ite, x) - bisect_left(ite, x))

H, W, M = map(int,input().split())
targets = [list(map(lambda x: int(x) - 1, input().split())) for i in range(M)]

cnth = [0] * H
cntw = [0] * W
wlist = [[] for i in range(H)]

for i in range(M):
	hi, wi = targets[i]
	cnth[hi] += 1
	cntw[wi] += 1
	wlist[hi].append(wi)

maxhi = []
maxwi = []
maxh = max(cnth)
maxw = max(cntw)

for i in range(H):
	wlist[i].sort()
	if cnth[i] == maxh:
		maxhi.append(i)
for i in range(W):
	if cntw[i] == maxw:
		maxwi.append(i)

for i in range(len(maxhi)):
	for j in range(len(maxwi)):
		if not exists(wlist[maxhi[i]], maxwi[j]):
			print(maxh + maxw)
			exit()
print(maxh + maxw - 1)

Solution (Python):
H, W, M = list(map(int, input().split()))
hrz = [0]*H
vrt = [0]*W
targets = set()
for _ in range(M):
    h, w = list(map(lambda x: int(x) - 1, input().split()))
    targets.add((h, w))
    hrz[h] += 1
    vrt[w] += 1

hrz_max = max(hrz)
vrt_max = max(vrt)
hrz_max_lines = [i for i in range(H) if hrz[i] == hrz_max]
vrt_max_lines = [i for i in range(W) if vrt[i] == vrt_max]
for h in hrz_max_lines:
    for w in vrt_max_lines:
        if not (h, w) in targets:
            print(hrz_max+vrt_max)
            exit()
print(hrz_max+vrt_max-1)

Solution (Python):

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

HSum=[0 for _ in range(H)]
WSum=[0 for _ in range(W)]


bombs = set()
for _ in range(M):
	hi,wi = map(lambda x:int(x)-1,input().split())
	HSum[hi] += 1
	WSum[wi] += 1
	bombs.add( (hi,wi) )





curMax = 0










hMax = max(HSum)
wMax = max(WSum)
tmpMax = hMax + wMax

ans = 0

hSumMaxOnly = [h for h, x in enumerate(HSum) if x == hMax]
wSumMaxOnly = [w for w, y in enumerate(WSum) if y == wMax]

for h in hSumMaxOnly:
	if ans == tmpMax:
		break
	for w in wSumMaxOnly:
		if (h,w) in bombs:
			ans = tmpMax - 1
		else:
			ans = tmpMax
			break



print(ans)

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

h_cnt = [0] * H
w_cnt = [0] * W
hw = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(M)]
for h, w in hw:
    h_cnt[h] += 1
    w_cnt[w] += 1

max_h = max(h_cnt)
max_w = max(w_cnt)
hs = [i for i in range(H) if h_cnt[i]==max_h]
ws = [i for i in range(W) if w_cnt[i]==max_w]

ans = max_h+max_w-1
hw = set(hw)
for h in hs:
    for w in ws:
        if (h, w) not in hw:
            print(max_h+max_w)
            exit()
print(max_h+max_w-1)

Solution (Python):
H,W,M=map(int,input().split())
nh=[0]*H
nw=[0]*W
boms=set()
for _ in range(M):
    h,w=map(int,input().split())
    nh[h-1] += 1
    nw[w-1] += 1
    boms.add((h-1, w-1))

maxh = max(nh)
maxw = max(nw)

i_indexes=[]
for i in range(H):
    if nh[i] == maxh:
        i_indexes.append(i)
j_indexes=[]
for j in range(W):
    if nw[j] == maxw:
        j_indexes.append(j)

for i in i_indexes:
    for j in j_indexes:
        if (i,j) in boms:
            continue
        print(maxh + maxw)
        exit()
print(maxh + maxw - 1)

Solution (Python):
import sys
input = sys.stdin.readline

H,W,M=map(int,input().split())
nh=[0]*H
nw=[0]*W
boms=set()
for _ in range(M):
    h,w=map(int,input().split())
    nh[h-1] += 1
    nw[w-1] += 1
    boms.add((h-1, w-1))

maxh = max(nh)
maxw = max(nw)

i_indexes=[]
for i in range(H):
    if nh[i] == maxh:
        i_indexes.append(i)
j_indexes=[]
for j in range(W):
    if nw[j] == maxw:
        j_indexes.append(j)

for i in i_indexes:
    for j in j_indexes:
        if (i,j) in boms:
            continue
        print(maxh + maxw)
        exit()
print(maxh + maxw - 1)

Solution (Python):
h,w,m = map(int,input().split())
t = [tuple(map(lambda x:int(x)-1,input().split())) for _ in range(m)] 
ht = [0]*h
wt = [0]*w
for y,x in t:
  ht[y] += 1
  wt[x] += 1
htm = max(ht)
wtm = max(wt)
htt = [i for i in range(h) if ht[i]==htm]
wtt = [i for i in range(w) if wt[i]==wtm]
ans = htm+wtm-1
t = set(t)
for i in htt:
  for j in wtt:
    if (i,j) not in t:
      print(htm+wtm)
      exit()
print(htm+wtm-1)

Solution (Python):
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


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

    row = [0] * H
    col = [0] * W
    bomb = set()

    for i in range(M):
        h, w = map(int, input().split())
        h -= 1
        w -= 1
        row[h] += 1
        col[w] += 1
        bomb.add((h, w))

    

    rowmax = max(row)
    colmax = max(col)

    rows = []
    cols = []
    for i in range(H):
        if row[i] == rowmax:
            rows.append(i)
    for i in range(W):
        if col[i] == colmax:
            cols.append(i)

    for r in rows:
        for c in cols:
            if (r, c) in bomb:
                continue
            else:
                print(rowmax + colmax)
                exit()

    print(rowmax + colmax - 1)


if __name__ == "__main__":
    main()