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):
import base64

exec(base64.b64decode(b'aW1wb3J0IHN1YnByb2Nlc3MKaW1wb3J0IHN5cwoKY29kZSA9IHIiIiIjaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Yml0c2V0PgojaW5jbHVkZSA8Y2Fzc2VydD4KI2luY2x1ZGUgPGNjdHlwZT4KI2luY2x1ZGUgPGNocm9ubz4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y29tcGxleD4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxkZXF1ZT4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPG51bWVyaWM+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPHJhbmRvbT4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dHVwbGU+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnVzaW5nIGludDY0ID0gbG9uZyBsb25nOwoKI2RlZmluZSBhbGwoXykgYmVnaW4oXyksIGVuZChfKQojZGVmaW5lIHJhbGwoXykgcmJlZ2luKF8pLCByZW5kKF8pCgppbnQgbWFpbigpIHsKICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICBjaW4udGllKG51bGxwdHIpOwoKICBpbnQgaCwgdywgbTsgY2luID4+IGggPj4gdyA+PiBtOwogIHZlY3RvcjxwYWlyPGludCwgaW50Pj4gaW47CgogIHZlY3RvcjxpbnQ+IGNudF9yb3coaCwgMCksIGNudF9jb2wodywgMCk7CgogIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKSB7CiAgICBpbnQgaGgsIHd3OyBjaW4gPj4gaGggPj4gd3c7CiAgICAtLWhoOyAtLXd3OwogICAgaW4uZW1wbGFjZV9iYWNrKGhoLCB3dyk7CiAgICArK2NudF9yb3dbaGhdOyArK2NudF9jb2xbd3ddOwogIH0KCiAgaW50IG14X3JvdyA9ICptYXhfZWxlbWVudChhbGwoY250X3JvdykpOwogIGludCBteF9jb2wgPSAqbWF4X2VsZW1lbnQoYWxsKGNudF9jb2wpKTsKICBpbnQgYW5zID0gbXhfcm93ICsgbXhfY29sOwogIGludDY0IGNhbiA9IGNvdW50KGFsbChjbnRfcm93KSwgbXhfcm93KSAqIGNvdW50KGFsbChjbnRfY29sKSwgbXhfY29sKTsKICBpZiAoY2FuID4gbSkgewogICAgY291dCA8PCBhbnMgPDwgZW5kbDsKICB9IGVsc2UgewogICAgaW50IGJhZCA9IDA7CiAgICBmb3IgKGF1dG8gZSA6IGluKSBiYWQgKz0gKGNudF9yb3dbZS5maXJzdF0gKyBjbnRfY29sW2Uuc2Vjb25kXSA9PSBhbnMpOwogICAgaWYgKGNhbiA+IGJhZCkgewogICAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgfSBlbHNlIHsKICAgICAgY291dCA8PCBhbnMgLSAxIDw8IGVuZGw7CiAgICB9CiAgfQoKICByZXR1cm4gMDsKfQoiIiIKCndpdGggb3Blbignc29sLmNwcCcsICd3JykgYXMgZjoKICAgIGYud3JpdGUoY29kZSkKCnN1YnByb2Nlc3MuUG9wZW4oWydnKysnLCAnLXN0ZD1jKysxNycsICctTzInLCAnc29sLmNwcCddKS5jb21tdW5pY2F0ZSgpCnN1YnByb2Nlc3MuUG9wZW4oWycuL2Eub3V0J10sIHN0ZGluPXN5cy5zdGRpbiwgc3Rkb3V0PXN5cy5zdGRvdXQpLmNvbW11bmljYXRlKCk='))

Solution (Python):
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def NLI(n):return [[int(i) for i in input().split()] for i in range(n)]
def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def Rtest(T):
    case,err=0,0
    for i in range(T):
        inp=INP()
        a1,ls=naive(*inp)
        a2=solve(*inp)
        if a1!=a2:
            print((a1,a2),inp)
            err+=1
        case+=1
    print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'
    
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase

read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()


show_flg=False
show_flg=True

ans=0

for _ in range(1):
    h,w,m=LI()
    b=NLI_(m)
    r=[0]*h
    c=[0]*w
    for x,y in b:
        r[x]+=1
        c[y]+=1
    mr=max(r)
    mc=max(c)
    cnt=0
    for x,y in b:
        if r[x]==mr and c[y]==mc:
            cnt+=1
    ans=mr+mc
    if cnt==r.count(mr)*c.count(mc):
        ans-=1
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()


def main():
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0] * H
    ws = [0] * W
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append(tuple([h-1, w-1]))
        hs[h-1] += 1
        ws[w-1] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxhindex = [i for i, x in enumerate(hs) if x == maxh]
    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    bombs = set(bombs)
    for i in maxhindex:
        for j in maxwindex:
            if (i, j) not in bombs:
                print(ans)
                exit()
    print(ans-1)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
H,W,M=map(int,input().split())
s={tuple(map(lambda x: int(x)-1, input().split())) for i in range(M)}
lh=[0]*H
lw=[0]*W
for i in s:
    lh[i[0]]+=1
    lw[i[1]]+=1
mh=max(lh)
mw=max(lw)
sh=[]
sw=[]
for i in range(H):
    if lh[i]==mh:
        sh.append(i)
for i in range(W):
    if lw[i]==mw:
        sw.append(i)
if len(sh)*len(sw)>M:
    print(mh+mw)
else:
    for i in sh:
        for j in sw:
            if (i,j) not in s:
                print(mh+mw)
                break
        else:
            continue
        break
    else:
        print(mh+mw-1)

Solution (Python):

H, W, M = map(int, input().split())
bombs = []
for _ in range(M):
    bomb = tuple(map(lambda x:int(x)-1, input().split()))
    bombs.append(bomb)

H_list = [0]*H
W_list = [0]*W
for h,w in bombs:
    H_list[h] += 1
    W_list[w] += 1
    
maxX = max(H_list)
maxY = max(W_list)

max_H = [i for i,k in enumerate(H_list) if k == maxX]
max_W = [i for i,k in enumerate(W_list) if k == maxY]

bombs = set(bombs)
for i in max_H:
    for k in max_W:
        if (i,k) not in bombs:
            print(maxX + maxY)
            exit()

print(maxX + maxY -1)

Solution (Python):
import sys


H, W, M = map(int, input().split())
bomb =[]
for I in range(M):
    bom = tuple(map(lambda x: int(x) - 1, input().split()))
    bomb.append(bom)
X = [0] * H  
Y = [0] * W  
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]  
C = [w for w, y in enumerate(Y) if y == maxY]  

bomb = set(bomb)
for r in R:
    for c in C:
        if (r, c) not in bomb:
            
            
            print(maxX + maxY)
            exit()
print(maxX + maxY - 1)

Solution (Python):
import sys


H, W, M = map(int, input().split())
bomb =[]
for I in range(M):
    bom = tuple(map(lambda x: int(x) - 1, input().split()))
    bomb.append(bom)
X = [0] * H  
Y = [0] * W  
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]  
C = [w for w, y in enumerate(Y) if y == maxY]  

bomb = set(bomb)
for r in R:
    for c in C:
        if (r, c) not in bomb:
            
            
            print(maxX + maxY)
            exit()
print(maxX + maxY - 1)

Solution (Python):
import sys


H, W, M = map(int, input().split())
bomb = [tuple(map(lambda x: int(x) - 1, s.split())) for s in sys.stdin.readlines()]
X = [0] * H  
Y = [0] * W  
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]  
C = [w for w, y in enumerate(Y) if y == maxY]  

bomb = set(bomb)
for r in R:
    for c in C:
        if (r, c) not in bomb:
            
            
            print(maxX + maxY)
            exit()
print(maxX + maxY - 1)

Solution (Python):


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

Hmax_temp = [0]*(H+1)
Wmax_temp = [0]*(W+1)
Hmax = 0
Wmax = 0
Bomb = dict()
for i in range(M):
	H_M,W_M = map(int,input().split())
	Hmax_temp[H_M] += 1
	Wmax_temp[W_M] += 1
	Bomb[(H_M,W_M)] = 1

Hmax = max(Hmax_temp)
Wmax = max(Wmax_temp)


y_max = []
for i in range(H+1):
	if Hmax == Hmax_temp[i]:
		y_max.append(i)
		

x_max = []
for j in range(W+1):
	if Wmax == Wmax_temp[j]:
		x_max.append(j)


flag = False
for i in y_max:
	for  j in x_max:
		if (i,j) not in Bomb:
			flag = True
			break
	if flag:
		break


if flag:
	ans = Wmax + Hmax
else:
	ans = Wmax + Hmax - 1
print(ans)

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

r=[0]*h
c=[0]*w
s=set()
for _ in range(m):
    i,j=map(int,input().split())
    i-=1
    j-=1
    r[i]+=1
    c[j]+=1
    s.add(i*w+j)

rmx=max(r)
cmx=max(c)
rc=[i for i in range(h) if r[i]==rmx]
cc=[j for j in range(w) if c[j]==cmx]

ans=rmx+cmx-1
for i in rc:
    for j in cc:
        if i*w+j not in s:
            print(ans+1)
            exit()

print(ans)

Solution (Python):
H, W, M = map(int, input().split())
hallline = [0 for i in range(H)]
wallline = [0 for i in range(W)]
b = {}

for i in range(M):
    tmp = list(map(lambda x: int(x) - 1, input().split()))
    hallline[tmp[0]] += 1
    wallline[tmp[1]] += 1
    b[(tmp[0], tmp[1])] = True

t = max(hallline)
s = max(wallline)

a = [i for i in range(H) if hallline[i] == t]
c = [i for i in range(W) if wallline[i] == s]

tmp = False

for i in a:
    for j in c:
        if (i, j) not in b.keys():
            print(t + s)
            tmp = True
            break
    if tmp:
        break
if not tmp:
    print(t + s - 1)

Solution (Python):
import numpy as np
from collections import defaultdict

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

rowsum = np.zeros(h, dtype=int)
colsum = np.zeros(w, dtype=int)
targets = defaultdict(int)
for _ in range(m):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    rowsum[hi] += 1
    colsum[wi] += 1
    targets[(hi, wi)] = 1

rowmax = np.max(rowsum)
colmax = np.max(colsum)
rowindices = np.where(rowsum == rowmax)[0]
colindices = np.where(colsum == colmax)[0]

for ri in rowindices:
    for ci in colindices:
        if targets[(ri, ci)]:
            continue
        print(rowmax + colmax)
        exit()

print(rowmax + colmax - 1)

Solution (Python):
from collections import defaultdict

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

rowsum = [0] * h
rowmax = 0
colsum = [0] * w
colmax = 0
targets = defaultdict(int)
for _ in range(m):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    rowsum[hi] += 1
    rowmax = max(rowmax, rowsum[hi])
    colsum[wi] += 1
    colmax = max(colmax, colsum[wi])
    targets[(hi, wi)] = 1

rowindices = []
for i, v in enumerate(rowsum):
    if v == rowmax:
        rowindices.append(i)

colindices = []
for i, v in enumerate(colsum):
    if v == colmax:
        colindices.append(i)

ans = rowmax + colmax - 1
for ri in rowindices:
    for ci in colindices:
        if targets[(ri, ci)]:
            continue
        print(rowmax + colmax)
        exit()

print(ans)

Solution (Python):
import sys


def solve():
    readline = sys.stdin.readline
    H, W, M = map(int, readline().split())
    h = [0] * H
    w = [0] * W
    b = set()
    for _ in range(M):
        y, x = map(int, readline().split())
        x -= 1
        y -= 1
        h[y] += 1
        w[x] += 1
        b.add(y * W + x)
    h = sorted(zip(h, range(H)), reverse=True)
    ans = 0
    for i in range(W):
        x = w[i]
        for y, j in h:
            if x + y > ans:
                if j * W + i in b:
                    ans = x + y - 1
                else:
                    ans = x + y
                    break
            else:
                break
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from collections import defaultdict

input = sys.stdin.readline

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

hor = [0] * H
ver = [0] * W
f = defaultdict(int)
for _ in range(m):
    h, w = map(int, input().split())
    hor[h - 1] += 1
    ver[w - 1] += 1
    f[str(h - 1) + '|' + str(w - 1)] = 1

hmax = max(hor)
hs = [i for i, v in enumerate(hor) if v == hmax]
vmax = max(ver)
vs = [i for i, v in enumerate(ver) if v == vmax]


ans = hmax + vmax

if len(hs) * len(vs) > m:
    print(ans)
    exit()

for k1 in hs:
    for k2 in vs:
        if f[str(k1) + '|' + str(k2)] == 0:
            print(ans)
            exit()
print(ans - 1)

Solution (Python):
import sys

def solve():
    readline = sys.stdin.readline
    H, W, M = map(int, readline().split())
    h = [0] * H
    w = [0] * W
    b = set()
    for _ in range(M):
        y, x = map(int, readline().split())
        x -= 1
        y -= 1
        h[y] += 1
        w[x] += 1
        b.add(y*W+x)
    h = sorted(zip(h, range(H)), reverse=True)
    ans = 0
    for i in range(W):
        x = w[i]
        for y, j in h:
            if x+y > ans:
                if j*W+i in b:
                    ans = x+y-1
                else:
                    ans = x+y
                    break
            else:
                break
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
import sys

def solve():
    readline = sys.stdin.readline
    H, W, M = map(int, readline().split())
    h = [0] * H
    w = [0] * W
    b = set()
    for _ in range(M):
        y, x = map(int, readline().split())
        x -= 1
        y -= 1
        h[y] += 1
        w[x] += 1
        b.add(y*W+x)
    h = sorted(zip(h, range(H)), reverse=True)
    ans = 0
    for i in range(W):
        x = w[i]
        for y, j in h:
            if x+y > ans:
                if j*W+i in b:
                    ans = x+y-1
                else:
                    ans = x+y
                    break
            else:
                break
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
from collections import Counter

H, W, M = map(int, input().split())
dh, dw = Counter(), Counter()
used = set()

for _ in range(M):
    h, w = map(int, input().split())
    dh[h] += 1
    dw[w] += 1
    used.add((h, w))

ih = dh.most_common()
iw = dw.most_common()
s = ih[0][1] + iw[0][1]
ans = 0

for h, sh in ih:
    for w, sw in iw:
        if sh+sw < s or ans == s:
            break
        b = sh + sw - ((h, w) in used)
        ans = max(ans, b)
print(ans)

Solution (Python):
from collections import Counter

H, W, M = map(int, input().split())
dh, dw = Counter(), Counter()
used = set()

for _ in range(M):
    h, w = map(int, input().split())
    dh[h] += 1
    dw[w] += 1
    used.add((h, w))

ih = dh.most_common()
iw = dw.most_common()
s = ih[0][1] + iw[0][1]
ans = 0

for h, sh in ih:
    for w, sw in iw:
        if sh+sw < s or ans == s:
            break
        b = sh + sw - ((h, w) in used)
        ans = max(ans, b)
print(ans)

Solution (Python):
from collections import Counter


def solve():
    H, W, M = map(int, input().split())
    dh, dw = Counter(), Counter()
    used = set()

    for _ in range(M):
        h, w = map(int, input().split())
        dh[h] += 1
        dw[w] += 1
        used.add((h, w))

    ih = dh.most_common()
    iw = dw.most_common()

    h, counth = ih[0]
    w, countw = iw[0]

    s = counth + countw
    ans = counth + countw - ((h, w) in used)

    for h, counth in ih:
        for w, countw in iw:
            if counth+countw < s or ans == s:
                break
            b = counth + countw - ((h, w) in used)
            ans = max(ans, b)
    print(ans)


solve()

Solution (Python):
h,w,m=map(int,input().split())
s=[0]*m
t=[0]*m
temp=set()
for i in range(m):
  sm,tm=map(int,input().split())
  s[i]=sm
  t[i]=tm
  temp.add((s[i],t[i]))
st=[0]*h
tt=[0]*w
for i in range(m):
  st[s[i]-1]=st[s[i]-1]+1
  tt[t[i]-1]=tt[t[i]-1]+1
smax=max(st)
tmax=max(tt)
ans=smax+tmax
maxs=[]
maxt=[]
for i in range(h):
  if st[i]==smax:
    maxs.append(i)
for i in range(w):
  if tt[i]==tmax:
    maxt.append(i)

if len(maxs)*len(maxt)>m:
  print(ans)
else:
  for i in range(len(maxs)):
    for j in range(len(maxt)):
      if (maxs[i]+1,maxt[j]+1) not in temp:
        print(ans)
        exit()
  else:
    print(ans-1)

Solution (Python):
import collections
import sys
def input() : return sys.stdin.readline().strip()
h,w,m = map(int,input().split())
X = []
Y = []
for _ in range(m):
    x,y = map(int,input().split())
    X.append(x)
    Y.append(y)

most_common_x = collections.Counter(X).most_common()
most_common_x_index = most_common_x[0][0]
most_common_x_count = most_common_x[0][1]
most_common_y = collections.Counter(Y).most_common()
most_common_y_index = most_common_y[0][0]
most_common_y_count = most_common_y[0][1]

otherwisey = []
otherwisex = []
for i in range(m):
    if X[i] != most_common_x_index:
        otherwisey.append(Y[i])
    if Y[i] != most_common_y_index:
        otherwisex.append(X[i])

most_common_y = collections.Counter(otherwisey).most_common()
if len(most_common_y)!= 0:
    ansx = most_common_y[0][1] + most_common_x_count
else:
    ansx = most_common_x_count

most_common_x = collections.Counter(otherwisex).most_common()
if len(most_common_x)!= 0:
    ansy = most_common_x[0][1] + most_common_y_count
else:
    ansy = most_common_y_count

print(max(ansx,ansy))

Solution (Python):
import collections

h,w,m = map(int,input().split())
X = []
Y = []
for _ in range(m):
    x,y = map(int,input().split())
    X.append(x)
    Y.append(y)

most_common_x = collections.Counter(X).most_common()
most_common_x_index = most_common_x[0][0]
most_common_x_count = most_common_x[0][1]
most_common_y = collections.Counter(Y).most_common()
most_common_y_index = most_common_y[0][0]
most_common_y_count = most_common_y[0][1]

otherwisey = []
otherwisex = []
for i in range(m):
    if X[i] != most_common_x_index:
        otherwisey.append(Y[i])
    if Y[i] != most_common_y_index:
        otherwisex.append(X[i])

most_common_y = collections.Counter(otherwisey).most_common()
if len(most_common_y)!= 0:
    ansx = most_common_y[0][1] + most_common_x_count
else:
    ansx = most_common_x_count

most_common_x = collections.Counter(otherwisex).most_common()
if len(most_common_x)!= 0:
    ansy = most_common_x[0][1] + most_common_y_count
else:
    ansy = most_common_y_count

print(max(ansx,ansy))

Solution (Python):
import collections

h,w,m = map(int,input().split())
X = []
Y = []
for _ in range(m):
    x,y = map(int,input().split())
    X.append(x)
    Y.append(y)

most_common_x = collections.Counter(X).most_common()
most_common_x_index = most_common_x[0][0]
most_common_x_count = most_common_x[0][1]
most_common_y = collections.Counter(Y).most_common()
most_common_y_index = most_common_y[0][0]
most_common_y_count = most_common_y[0][1]

otherwisey = []
otherwisex = []
for i in range(m):
    if X[i] != most_common_x_index:
        otherwisey.append(Y[i])
    if Y[i] != most_common_y_index:
        otherwisex.append(X[i])

most_common_y = collections.Counter(otherwisey).most_common()
if len(most_common_y)!= 0:
    ansx = most_common_y[0][1] + most_common_x_count
else:
    ansx = most_common_x_count

most_common_x = collections.Counter(otherwisex).most_common()
if len(most_common_x)!= 0 :
    ansy = most_common_x[0][1] + most_common_y_count
else:
    ansy = most_common_y_count

print(max(ansx,ansy))

Solution (Python):
import collections

h,w,m = map(int,input().split())
X = []
Y = []
for _ in range(m):
    x,y = map(int,input().split())
    X.append(x)
    Y.append(y)

most_common_x = collections.Counter(X).most_common()
most_common_x_index = most_common_x[0][0] 
most_common_x_count = most_common_x[0][1] 

otherwise = []
for i in range(m):
    if X[i] != most_common_x_index:
        otherwise.append(Y[i])
most_common_y = collections.Counter(otherwise).most_common()
if len(most_common_y)!= 0: 
    ansx = most_common_y[0][1] + most_common_x_count
else: 
    ansx = most_common_x_count

most_common_y = collections.Counter(Y).most_common()
most_common_y_index = most_common_y[0][0] 
most_common_y_count = most_common_y[0][1] 
otherwise = []
for i in range(m):
    if Y[i] != most_common_y_index:
        otherwise.append(X[i])
most_common_x = collections.Counter(otherwise).most_common()
if len(most_common_x)!= 0 :
    ansy = most_common_x[0][1] + most_common_y_count
else:
    ansy = most_common_y_count
print(max(ansx,ansy))

Solution (Python):

import collections
import sys
import math
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())
def NIJIGEN(H): return [list(input()) for i in range(H)]
H,W,M=map(int,input().split())
A=list()
B=list()
for i in range(M):
  a,b=map(int,input().split())
  A.append(a)
  B.append(b)
c = collections.Counter(A)
s=c.most_common()
k=s[0][0]
t=s[0][1]
L=list()
for i in range(M):
  if A[i]!=k:
    L.append(B[i])
c = collections.Counter(L)
s=c.most_common()
if len(s)!=0:
  ans=s[0][1]+t
else:
  ans=t
c = collections.Counter(B)
s=c.most_common()
k=s[0][0]
t=s[0][1]
L=list()
for i in range(M):
  if B[i]!=k:
    L.append(A[i])
c = collections.Counter(L)
s=c.most_common()
if len(s)!=0:
  q=s[0][1]+t
else:
  q=t
ans=max(ans,q)
print(ans)

Solution (Python):
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
@njit((i8[:],i8[:],i8[:],i8[:]), cache=True)
def main(lis_h,lis_w,bomb_h,bomb_w):
  bomb = {}
  for h,w in zip(bomb_h,bomb_w):
    bomb[(h,w)] = 1
  max_h = np.where(lis_h==lis_h.max())[0]
  max_w = np.where(lis_w==lis_w.max())[0]
  ans = lis_h.max()+lis_w.max()
  for h in max_h:
    for w in max_w:
      if (h,w) not in bomb:
        return ans
  return ans-1

H, W, M = map(int, input().split())
bombh = np.zeros(M, np.int64)
bombw = np.zeros(M, np.int64)
lis_h = np.zeros(H, np.int64)
lis_w = np.zeros(W, np.int64)
for i in range(M):
    h,w = map(int, input().split())
    h -= 1
    w -= 1
    bombh[i], bombw[i] = h,w
    lis_h[h] += 1
    lis_w[w] += 1
max_h = np.where(lis_h==lis_h.max())
max_w = np.where(lis_w==lis_w.max())
print(main(lis_h,lis_w,bombh,bombw))

Solution (Python):
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
@njit((i8[:],i8[:],i8[:],i8[:]), cache=True)
def main(lis_h,lis_w,bomb_h,bomb_w):
  bomb = {}
  for h,w in zip(bomb_h,bomb_w):
    bomb[(h,w)] = 1
  m_h = max(lis_h)
  m_w = max(lis_w)
  m_h_lis = []
  m_w_lis = []
  for i,l in enumerate(lis_h):
    if l==m_h:
      m_h_lis.append(i)
  for i,l in enumerate(lis_w):
    if l==m_w:
      m_w_lis.append(i)
  ans = m_h+m_w
  for h in m_h_lis:
    for w in m_w_lis:
      if (h,w) not in bomb:
        return ans
  return ans-1

H, W, M = map(int, input().split())
bombh = np.zeros(M, np.int64)
bombw = np.zeros(M, np.int64)
lis_h = np.zeros(H, np.int64)
lis_w = np.zeros(W, np.int64)
for i in range(M):
    h,w = map(int, input().split())
    h -= 1
    w -= 1
    bombh[i], bombw[i] = h,w
    lis_h[h] += 1
    lis_w[w] += 1
print(main(lis_h,lis_w,bombh,bombw))

Solution (Python):
from collections import defaultdict
H,W,M = map(int,input().split())
L=[]
bomb = defaultdict(lambda: 0)
for i in range(M):
    h,w = map(int,input().split())
    L.append([h-1,w-1])
    bomb[(h-1,w-1)] = 1

h = dict()
w = dict()

for i in range(M):
    val = L[i][0]
    if val in h:
        h[val] += 1
    else:
        h[val] = 1

for i in range(M):
    val = L[i][1]
    if val in w:
        w[val] += 1
    else:
        w[val] = 1

maxh = []
maxw = []
m=-1
for val in h:
    if h[val]>m:
        m = h[val]

for val in h:
    if m==h[val]:
        maxh.append(val)

ans = m
m=-1
for val in w:
    if w[val]>m:
        m=w[val]

for val in w:
    if m==w[val]:
        maxw.append(val)

ans += m


for mh in maxh:
    for mw in maxw:
        if bomb[(mh,mw)]==0:
            print(ans)
            exit()

print(ans-1)

Solution (Python):
(h,w,m),*e=[[*map(int,i.split())]for i in open(0)]
a,b=[0]*h,[0]*w
for y,x in e:
    a[y-1]+=1
    b[x-1]+=1
ans=max(a)+max(b)
c=a.count(max(a))*b.count(max(b))-sum(a[y-1]+b[x-1]==ans for y,x in e)
print(ans-(c<=0))

Solution (Python):
def main():
    h,w,m=map(int,input().split())
    target=set()
    for i in range(m):
        th,tw=map(int,input().split())
        target.add((th-1,tw-1))
    lines=[0]*w
    rows=[0]*h
    for th,tw in target:
        lines[tw]+=1
        rows[th]+=1
    max_lines=max(lines)
    max_rows=max(rows)
    max_wi=[i for i,wi in enumerate(lines) if wi==max_lines]
    max_hi=[i for i,hi in enumerate(rows) if hi==max_rows]
    
    ans=max_lines+max_rows-1
    isLoop=True
    for wi in max_wi:
        if isLoop:
            for hi in max_hi:
                if not (hi,wi) in target:
                    ans+=1
                    isLoop=False
                    break
    return ans

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

Solution (Python):
from collections import defaultdict
def solve():
  H, W, M = map(int, input().split())
  bomb = defaultdict(lambda: 0)
  lis_h = [0]*H
  lis_w = [0]*W
  for _ in range(M):
    h,w = map(int, input().split())
    h -= 1
    w -= 1
    bomb[(h,w)] = 1
    lis_h[h] += 1
    lis_w[w] += 1
  m_h = max(lis_h)
  m_w = max(lis_w)
  m_h_lis = []
  m_w_lis = []
  for i in range(H):
    if lis_h[i]==m_h:
      m_h_lis.append(i)
  for i in range(W):
    if lis_w[i]==m_w:
      m_w_lis.append(i)
  ans = 0
  for h in m_h_lis:
    for w in m_w_lis:
      if bomb[(h,w)]==0:
        return m_h+m_w
  return m_h+m_w-1
print(solve())

Solution (Python):
H,W,M=map(int,input().split())
L=[]
bh=[0]*(H+1)
bw=[0]*(W+1)
for _ in range(M):
    h,w=map(int,input().split())
    L.append((h,w))
    bh[h]+=1
    bw[w]+=1
p,q=max(bh),max(bw)
num=len(list(filter(p.__eq__, bh)))*len(list(filter(q.__eq__,bw)))
num-=len(list(filter(lambda x: bh[x[0]]==p and bw[x[1]]==q, L)))
ans=p+q-1
if num>0:
    ans+=1
print(ans)

Solution (Python):
def solve(H, W, M, h, w):
    f = [0] * (H+1)
    g = [0] * (W+1)
    for r, c in zip(h, w):
        f[r] += 1
        g[c] += 1
    p = max(f)
    q = max(g)
    num = len(list(filter(p.__eq__, f))) * len(list(filter(q.__eq__, g)))
    num -= len(list(filter(lambda _: f[_[0]] + g[_[1]] == p + q, zip(h, w))))
    return p + q - (num <= 0)

H, W, M = map(int, input().split())
h, w = zip(*[map(int, input().split()) for i in range(M)])
print(solve(H, W, M, h, w))

Solution (Python):
import numpy as np

def solve(H, W, M, h, w):
    f = [0] * (H+1)
    g = [0] * (W+1)
    for r, c in zip(h, w):
        f[r] += 1
        g[c] += 1
    p = np.max(f)
    q = np.max(g)
    num = len(list(filter(p.__eq__, f))) * len(list(filter(q.__eq__, g)))
    for r, c in zip(h, w):
        if (f[r] == p) and (g[c] == q):
            num -= 1
    return p + q - (num <= 0)

H, W, M = map(int, input().split())
h, w = zip(*[map(int, input().split()) for i in range(M)])
print(solve(H, W, M, h, w))

Solution (Python):
from collections import Counter
h, w, m = map(int, input().split())
blist = [list(map(int, input().split())) for _ in range(m)]

blist_set = set(map(tuple, blist))

rowdict = Counter([blist[i][0] for i in range(m)])
coldict = Counter([blist[i][1] for i in range(m)])

maxr = rowdict.most_common()[0][1]
maxr_keys = []
for i in rowdict.most_common():
    if i[1] == maxr:
        maxr_keys.append(i[0])
    else:
        break
maxc = coldict.most_common()[0][1]
maxc_keys = []
for i in coldict.most_common():
    if i[1] == maxc:
        maxc_keys.append(i[0])
    else:
        break

for i in maxr_keys:
    for j in maxc_keys:
        if (i, j) not in blist_set:
            print(maxr + maxc)
            break
    else:
        continue
    break
else:
    print(maxr + maxc - 1)

Solution (Python):
from collections import defaultdict
from operator import itemgetter
import sys
input = sys.stdin.readline

h, w, m = map(int, input().split())
bombs = [tuple(map(int, input().split())) for _ in range(m)]

x_cnt = defaultdict(int)
y_cnt = defaultdict(int)
for x, y in bombs:
    x_cnt[x] += 1
    y_cnt[y] += 1

x_cnt_max = max(x_cnt.values())
y_cnt_max = max(y_cnt.values())
cnt = cnt_x = cnt_y = 0
for x, y in bombs:
    if x_cnt[x] == x_cnt_max and y_cnt[y] == y_cnt_max:
        cnt += 1

cnt_x = sum(v == x_cnt_max for v in x_cnt.values())
cnt_y = sum(v == y_cnt_max for v in y_cnt.values())
if cnt_x * cnt_y == cnt:
    ans = x_cnt_max + y_cnt_max - 1
else:
    ans = x_cnt_max + y_cnt_max


print(ans)

Solution (Python):

import sys
input=sys.stdin.readline

h,w,m=map(int,input().split())
hs=dict()
ws=dict()
hw=set()
for i in range(m):
    hi,wi=map(int,input().split())
    if hi-1 in ws:
        ws[hi-1]+=1
    else:
        ws[hi-1]=1
    if wi-1 in hs:
        hs[wi-1]+=1
    else:
        hs[wi-1]=1
    hw.add((hi-1,wi-1))    

wmax=max(list(ws.values()))
hh=[]
for ik,wv in ws.items():
    if wv==wmax:
        hh.append(ik)
hmax=max(list(hs.values()))
ww=[]
for jk,hv in hs.items():
    if hv==hmax:
        ww.append(jk)
        
for hi in hh:
    for wi in ww:
        if  not ((hi,wi) in hw):

            print(wmax+hmax) 
            sys.exit()
print(wmax+hmax-1)