In [2]:
import time
def find_path(n,l,all_path):
    start_time = time.time()
    result= []
    paths = []
    go_down = 0
    for path in all_path:
        floor = path[0]
        
        for i in range(len(path)):
            if sum(path)/n == path[0]:
                go_down = 0
                floor = 0
                result.append(True)
                paths.append(path)
                break
            if i == 0:
                continue
            if i == n-1:
                go_down = 0
                floor = 0
                result.append(True)
                paths.append(path)
                break 
            if i != 0:
                if path[i] == path[i+1] and path[i-1] == path[i]:
                    continue
                if path[i] == floor:
                    continue
                #내려가기
                if path[i] < floor:
                    if floor-path[i] >= 2:
                        break
                    if (i+l-1) > n-1:
                        break
                    else:
                        if sum(path[i:i+l])/ l != path[i]:
                            break
                        else:
                            floor = path[i]
                            go_down = i
                            continue
                #올라가기
                if path[i] > floor:
                    if path[i] - floor >= 2:
                        break
                    if i-l < 0:
                        break
                    else:
                        if go_down > 0:
                            if i-go_down < l*2:
                                go_down = 0
                                break
                        if go_down == 0:
                            if sum(path[i-l:i])/l != path[i-l]:
                                break
                            else:
                                floor = path[i]
                                continue
    finish_time = time.time()
    print(finish_time - start_time)
    return sum(result), paths
                        
                    
                        
            
    
n,l = list(map(int,input().split()))
all_path = []
for i in range(n):
    path = list(map(int,input().split()))
    all_path.append(path)
all_path2 = [list(x) for x in zip(*all_path)]
all_path = all_path + all_path2
print(find_path(n,l,all_path))


5 3
1 1 1 2 2
2 1 1 1 1
3 1 1 1 1
2 2 2 2 2
3 2 2 2 2
0.0
(6, [[1, 1, 1, 2, 2], [2, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 2, 2, 2, 2], [1, 1, 1, 2, 2], [1, 1, 1, 2, 2]])

In [5]:
n,l = list(map(int,input().split()))
all_path = []
for i in range(n):
    path = list(map(int,input().split()))
    all_path.append(path)
all_path2 = [list(x) for x in zip(*all_path)]
all_path = all_path + all_path2
print(find_path(n,l,all_path))


6 2
3 2 1 1 2 3
3 2 2 1 2 3
3 2 2 2 3 3
3 3 3 3 3 3
3 3 3 3 2 2
3 3 3 3 2 2
0.0
(7, [[3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 2, 2], [3, 3, 3, 3, 2, 2], [3, 3, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3], [2, 2, 3, 3, 2, 2], [3, 3, 3, 3, 2, 2]])

In [6]:
n,l = list(map(int,input().split()))
all_path = []
for i in range(n):
    path = list(map(int,input().split()))
    all_path.append(path)
all_path2 = [list(x) for x in zip(*all_path)]
all_path = all_path + all_path2
print(find_path(n,l,all_path))


6 3
3 2 1 1 2 3
3 2 2 1 2 3
3 2 2 2 3 3
3 3 3 3 3 3
3 3 3 3 2 2
3 3 3 3 2 2
0.0
(3, [[3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3]])

In [7]:
n,l = list(map(int,input().split()))
all_path = []
for i in range(n):
    path = list(map(int,input().split()))
    all_path.append(path)
all_path2 = [list(x) for x in zip(*all_path)]
all_path = all_path + all_path2
print(find_path(n,l,all_path))


6 1
3 2 1 1 2 3
3 2 2 1 2 3
3 2 2 2 3 3
3 3 3 3 3 3
3 3 3 3 2 2
3 3 3 3 2 2
0.0
(11, [[3, 2, 1, 1, 2, 3], [3, 2, 2, 2, 3, 3], [3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 2, 2], [3, 3, 3, 3, 2, 2], [3, 3, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3], [1, 2, 2, 3, 3, 3], [1, 1, 2, 3, 3, 3], [2, 2, 3, 3, 2, 2], [3, 3, 3, 3, 2, 2]])