Pretty sure the answer is no as a math problem where f is pure. If f returns the same answer each time, the square ending at N,N contains [1, N * N]. Then the rectangle ending at N,N+1 must contain (N * N, N * (N+1)] in the nonoverlapping strip. Ditto for N+1,N. But then for N+1,N+1 all other locations must contain low numbers and there's only a single corner cell left which can contain the rest of the missing numbers between (N(N+1), (N+1)(N+1)]. Unless I made a mistake I don't think any function would work here for N > 1. What were the examples you found?
OTOH as a programming problem you can just cheat and store state somewhere to count the row width. This satisfies your interface requirements (python):
lastJ = None
def f(i, j):
global lastJ
if i != 0:
return i * (lastJ + 1) + j
else:
lastJ = j
return j
N = 3
M = 5
seen = set()
for i in range(N):
for j in range(M):
q = f(i, j)
seen.add(q)
assert sorted(seen) == list(range(N * M))