Title: あみだくじ II

There are n vertical lines for a given amida. The amida satisfies the following conditions:
- Horizontal lines are drawn straight across and do not slant.
- Horizontal lines always connect adjacent vertical lines and do not cross over other vertical lines.
- No vertical line has horizontal lines coming out from both sides simultaneously.
- There is only one winning path.

The program should determine if it's possible to reach the winning path by adding a single horizontal line at any position in the given amida. The input consists of multiple datasets, each containing:
- The number of vertical lines, n (1 < n ≤ 10).
- The selected vertical line number, m (1 ≤ m ≤ n).
- The position of the winning path (represented by a star in the diagram).
- The number of levels in the amida, d (1 ≤ d ≤ 30).
- The arrangement of horizontal lines (1 for present, 0 for absent) between each pair of vertical lines from top to bottom.

The input ends with a line containing a single 0.

For each dataset, the program should output:
- 0 if it's possible to reach the winning path without adding a horizontal line.
- The position of the closest horizontal line to the starting side if adding one horizontal line allows reaching the winning path.
- 1 if adding a horizontal line still doesn't allow reaching the winning path.

Sample Input:
5
2
3
9
1010
1001
0100
1001
0010
1000
0100
0101
1010
0

Output for the Sample Input:
6 4

# Buggy version:
def check_lots(start, goal, line):
    for i in range(len(line)):
        for j in range(len(line[i])):
            if line[i][j] == 1:
                if j == start - 1:
                    start -= 1
                elif j == start:
                    start += 1
    return start == goal

while True:
    n = int(input())
    if n == 0:
        break
    m = int(input())
    s = int(input())
    d = int(input())
    l = []
    for i in range(d):
        l.append([0] + list(map(int, list(input()))) + [0])
    d_ans = 0
    n_ans = 0
    if check_lots(m, s, l):
        print(0)
    else:
        for i in range(d):
            for j in range(1, n):
                l_tmp = l[:]
                if sum(l_tmp[i][j-1:j+2]) > 0:
                    continue
                l_tmp[i][j] = 1
                if check_lots(m, s, l_tmp):
                    d_ans = i + 1
                    n_ans = j
                    break
            else:
                continue
            break
        if d_ans:
            print(d_ans, n_ans)
        else:
            print(1)



# Fixed version:
import copy

def check_lots(start, goal, line):
    for i in range(len(line)):
        for j in range(len(line[i])):
            if line[i][j] == 1:
                if j == start - 1:
                    start -= 1
                elif j == start:
                    start += 1
    return start == goal

while True:
    n = int(input())
    if n == 0:
        break
    m = int(input())
    s = int(input())
    d = int(input())
    l = []
    for i in range(d):
        l.append([0] + list(map(int, list(input()))) + [0])
    d_ans = 0
    n_ans = 0
    if check_lots(m, s, l):
        print(0)
    else:
        for i in range(d):
            for j in range(1, n):
                l_tmp = copy.deepcopy(l[:])
                if sum(l[i][j-1:j+2]) > 0:
                    continue
                l_tmp[i][j] = 1
                if check_lots(m, s, l_tmp):
                    d_ans = i + 1
                    n_ans = j
                    break
            else:
                continue
            break
        if d_ans:
            print(d_ans, n_ans)
        else:
            print(1)