Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
"technically each position can be visited multiple times but only contributes to your sum once". Obviously if K is larger than N*M (I'm not sure by how much, but a constant) then the problem becomes trivial,