|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + static int N, M; |
| 8 | + static int[][] board; |
| 9 | + static int maxSqrt = -1; |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + |
| 14 | + String[] sizes = br.readLine().split(" "); |
| 15 | + N = Integer.parseInt(sizes[0]); |
| 16 | + M = Integer.parseInt(sizes[1]); |
| 17 | + |
| 18 | + board = new int[N][M]; |
| 19 | + for (int i = 0; i < N; i++) { |
| 20 | + String line = br.readLine(); |
| 21 | + for (int j = 0; j < M; j++) { |
| 22 | + board[i][j] = line.charAt(j) - '0'; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + for (int row = 0; row < N; row++) { |
| 27 | + for (int col = 0; col < M; col++) { |
| 28 | + for (int dr = -N; dr <= N; dr++) { |
| 29 | + for (int dc = -M; dc <= M; dc++) { |
| 30 | + if (dr == 0 && dc == 0) continue; |
| 31 | + |
| 32 | + int r = row; |
| 33 | + int c = col; |
| 34 | + StringBuilder sb = new StringBuilder(); |
| 35 | + |
| 36 | + while (r >= 0 && r < N && c >= 0 && c < M) { |
| 37 | + sb.append(board[r][c]); |
| 38 | + long num = Long.parseLong(sb.toString()); |
| 39 | + |
| 40 | + if (isPerfectSquare(num)) { |
| 41 | + maxSqrt = Math.max(maxSqrt, (int) num); |
| 42 | + if (maxSqrt >= 31622 * 31622) { |
| 43 | + System.out.println(maxSqrt); |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + r += dr; |
| 49 | + c += dc; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + System.out.println(maxSqrt); |
| 57 | + } |
| 58 | + |
| 59 | + static boolean isPerfectSquare(long num) { |
| 60 | + long sqrt = (long) Math.sqrt(num); |
| 61 | + return sqrt * sqrt == num; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +``` |
0 commit comments