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): m = len(lb[l]) // 2 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)) for i,(bk,x,y) in zip(lb[l][:m], sorted(cands,reverse=True)): 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] cands = [] 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)) for i,(bk,x,y) in zip(lb[l][m:], sorted(cands,reverse=True)): 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] print(*ans, sep='\n')