-- Your code here!
import Control.Monad

solve :: [[Int]] -> [[Int]] -> [Int] -> [Int]
solve mas [] ls = ls
solve mas (a:aa) ls = solve mas aa (sum (map func mas):ls)
    where
        func (n1:n2:n3:n4:ns) = if (a !! (n2-1)) - (a !! (n1-1)) == n3 then n4 else 0
                
        
main = do
    [n,m,q] <- map read . words <$> getLine
    mas <- replicateM q $ map read . words <$> getLine :: IO [[Int]]
    
    let ans = maximum $ solve mas (replicateM n [1..m]) []
    print ans