It is often heard that the apple doesn’t fall far from the tree. But is that really so?
The National Statistics Department has tracked the falling of apples in a fruit garden for G consecutive years. The fruit garden can be represented as a matrix with dimensions R ·S. Each field of the matrix can contain more than one apple tree.
Interestingly enough, each year there was exactly one apple fall, so the Department decided to write down G pairs of numbers (ri , si) that denote the row and column of the location where the apple fell during the i-th year. Moreover, by next year, a new tree grew at that location.
Your task is to determine the squared distance between the nearest tree and the apple that fell, measured in unit fields of the matrix (we assume it is that tree from which the apple fell).
The distance between fields (r1, s1) and (r2, s2) in the matrix are calculated as:
입력 3 3 x.. ... ... 3 1 3 1 1 3 2 출력 4 0 5 입력 5 5 ..x.. ....x ..... ..... ..... 4 3 1 5 3 4 5 3 5 출력 8 8 4 1 Clarification of the first example: The closest apple to the one that fell in the first year is the apple in the field (1,1). The apple that fell in the second year fell on the exact field where the tree is located, so the squared distance is 0. The apple that fell in the third year is equally distant to all three existing trees in the fruit garden.
출처:coci 2013-2014 contest5 5/6