from collections import Counter N,K = map(int,input().split()) ls = list(map(int,input().split())) lbk = [[] for l in range(26)] for i,l in enumerate(ls): lbk[l].append(i) src = [list(map(int,list(input()))) for i in range(N)] ans = [None] * K for y,row in enumerate(src): seq = 0 for x,a in enumerate(row): if a: seq += 1 elif seq: if lbk[seq]: i = lbk[seq].pop() ans[i] = '{0} {1} {2} {3}'.format(y+1,x-seq+1,y+1,x) seq = 0 if seq and lbk[seq]: i = lbk[seq].pop() ans[i] = '{0} {1} {2} {3}'.format(y+1,N-seq+1,y+1,N) for i in range(K): if not ans[i]: l = ls[i] ans[i] = '{0} {1} {2} {3}'.format(N,1,N,l) print(*ans, sep='\n')