In this line:
terain[x1,y1] = 1;
You probably meant
[x, y]
(the variables you just randomly generated).
x1
and
y1
are the dimensions you specified earlier, so the "bottom-right" element of this array will be at
[x1 - 1, y1 - 1]
which makes
[x1, y1]
out of range.
[Edit]
You'll also have to increase
placedmas
at some point, or you'll get stuck in an infinite loop.