#升级简化了一下代码,求赞
#争当全网逻辑最简洁 一目了然的代码 无需注释
import sys
data = sys.stdin.readlines()
H = int(data[0].split(" ")[0])
L = int(data[0].split(" ")[1])
lj = data[1:]
for i in range(len(lj)):
lj[i] = lj[i].strip("\n")
lj[i] = lj[i].split(" ")
def walk(point, fx):
global new_point
if fx == "E":
new_point = [point[0], point[1] + 1]
elif fx == "N":
new_point = [point[0] - 1, point[1]]
elif fx == "S":
new_point = [point[0] + 1, point[1]]
elif fx == "W":
new_point = [point[0], point[1] - 1]
if new_point[0] < 0 or new_point[1] < 0:
return "xxx"
try:
value = lj[new_point[0]][new_point[1]]
if value == "0":
return [new_point[0], new_point[1]]
if value == "1":
return "xxx"
except:
return "xxx"
H = len(lj)
L = len(lj[0])
def KO(point, fx, lastresult):
new = walk(point, fx)
if new == [H - 1, L - 1]:
T = lastresult + [new]
for item in T:
print("({},{})".format(item[0], item[1]))
elif new != "xxx" and new not in lastresult:
dfs(new, lastresult + [new])
def dfs(point, lastresult):
KO(point, "E", lastresult)
KO(point, "S", lastresult)
KO(point, "N", lastresult)
KO(point, "W", lastresult)
dfs([0, 0], [[0, 0]])