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)) for i,(bk,x,y) in zip(lb[l], 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] print(*ans, sep='\n')