
function [p, pos] = grid (a, b)

     n = a*b;
     pos = zeros(n,2);
     p = zeros(n,n);
     i = 1;
     for x = 1:a
       for y = 1:b
         ind(x,y) = i;
         pos(i,1) = x;
         pos(i,2) = y;
         i = i+1;
       end
     end
     if (not(i-1==n)) 
        error('Error indexing unlabeled data: %d != %d', i-1, n)
     end

     for i = 1:n
        x = pos(i,1);
        y = pos(i,2);
        if (x > 1)
           j = ind(x-1,y);
           p(i,j) = 1;
        end
        if (x < a)
           j = ind(x+1,y);
           p(i,j) = 1;
        end
        if (y < b)
           j = ind(x,y+1);
           p(i,j) = 1;
        end
        if (y > 1)
           j = ind(x,y-1);
           p(i,j) = 1;
        end
     end

