N,K = map(int,input().split()) ls = list(map(int,input().split())) lb = [[] for l in range(26)] for i,l in enumerate(ls): lb[l].append(i) arr = [list(map(int,list(input()))) for i in range(N)] ans = [None] * K for l in range(25,0,-1): cands = [] for y,row in enumerate(arr): tmpbk = maxbk = sum(row[:l]) maxx = 0 for x in range(l,N): tmpbk += row[x] tmpbk -= row[x-l] if tmpbk > maxbk: maxbk = tmpbk maxx = x-l+1 if maxbk == l: break cands.append((maxbk, maxx, y, 0)) for x,col in enumerate(zip(*arr)): tmpbk = maxbk = sum(col[:l]) maxy = 0 for y in range(l,N): tmpbk += col[y] tmpbk -= col[y-l] if tmpbk > maxbk: maxbk = tmpbk maxy = y-l+1 if maxbk == l: break cands.append((maxbk, x, maxy, 1)) for i,(bk,x,y,isv) in zip(lb[l], sorted(cands,reverse=True)): if isv: ans[i] = '{0} {1} {2} {3}'.format(y+1,x+1,y+l,x+1) for _y in range(y,y+l): arr[_y][x] = 1 - arr[_y][x] else: ans[i] = '{0} {1} {2} {3}'.format(y+1,x+1,y+1,x+l) for _x in range(x,x+l): arr[y][_x] = 1 - arr[y][_x] print(*ans, sep='\n')