Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    h, w, m = map(int, input().split())
    h = [0] * m
    w = [0] * m
    object_set = set()
    for i in range(m):
        h[i], w[i] = map(int, input().split())
        object_set.add((h[i], w[i]))
    h_counter = Counter(h)
    w_counter = Counter(w)
    max_h_num = h_counter.most_common()[0][1]
    max_w_num = w_counter.most_common()[0][1]
    ans = max_h_num + max_w_num

    h_cand = []
    w_cand = []
    for e, n in h_counter.most_common():
        if n == max_h_num:
            h_cand.append(e)
    for e, n in w_counter.most_common():
        if n == max_w_num:
            w_cand.append(e)

    ok = False
    for h_e in h_cand:
        for w_e in w_cand:
            if (h_e, w_e) not in object_set:
                ok = True

    print(ans if ok else ans - 1)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    h, w, m = map(int, input().split())
    h = [0] * m
    w = [0] * m
    object_set = set()
    for i in range(m):
        h[i], w[i] = map(int, input().split())
        object_set.add((h[i], w[i]))
    h_counter = Counter(h)
    w_counter = Counter(w)
    max_h_num = h_counter.most_common()[0][1]
    max_w_num = w_counter.most_common()[0][1]
    ans = max_h_num + max_w_num

    h_cand = set()
    w_cand = set()
    for e, n in h_counter.most_common():
        if n == max_h_num:
            h_cand.add(e)
    for e, n in w_counter.most_common():
        if n == max_w_num:
            w_cand.add(e)

    cnt = 0
    for h_e, w_e in object_set:
        if h_e in h_cand and w_e in w_cand:
            cnt += 1

    print(ans if cnt < len(h_cand) * len(w_cand) else ans - 1)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    h, w, m = map(int, input().split())
    h = [0] * m
    w = [0] * m
    object_set = set()
    for i in range(m):
        h[i], w[i] = map(int, input().split())
        object_set.add((h[i], w[i]))
    h_counter = Counter(h)
    w_counter = Counter(w)
    max_h_num = h_counter.most_common()[0][1]
    max_w_num = w_counter.most_common()[0][1]
    ans = max_h_num + max_w_num

    h_cand = []
    w_cand = []
    for e, n in h_counter.most_common():
        if n == max_h_num:
            h_cand.append(e)
    for e, n in w_counter.most_common():
        if n == max_w_num:
            w_cand.append(e)

    ok = False
    for h_e in h_cand:
        for w_e in w_cand:
            if (h_e, w_e) not in object_set:
                ok = True

    print(ans if ok else ans - 1)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    h, w, m = map(int, input().split())
    h = [0] * m
    w = [0] * m
    object_set = set()
    for i in range(m):
        h[i], w[i] = map(int, input().split())
        object_set.add((h[i], w[i]))
    h_counter = Counter(h)
    w_counter = Counter(w)
    max_h_num = h_counter.most_common()[0][1]
    max_w_num = w_counter.most_common()[0][1]
    ans = max_h_num + max_w_num

    h_cand = set()
    w_cand = set()
    for e, n in h_counter.most_common():
        if n == max_h_num:
            h_cand.add(e)
    for e, n in w_counter.most_common():
        if n == max_w_num:
            w_cand.add(e)

    cnt = 0
    for h_e, w_e in object_set:
        if h_e in h_cand and w_e in w_cand:
            cnt += 1

    print(ans if cnt < len(h_cand) * len(w_cand) else ans - 1)


if __name__ == "__main__":
    main()