오른쪽 -> 아래 -> 왼쪽 -> 위 -> ...
의 움직임이 반복됩니다.import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int M = Integer.parseInt(s[0]);
int N = Integer.parseInt(s[1]);
int x = 0;
int y = 0;
int[][] board = new int[M][N];
int[][] direction = {{0,1},{1,0},{0,-1},{-1,0}};
int d = 0;
int cnt = 0;
int answer = 0;
while (cnt<M*N) {
board[x][y] = ++cnt;
if (0<=x+direction[d][0] && x+direction[d][0]<M && 0<=y+direction[d][1] && y+direction[d][1]<N && board[x+direction[d][0]][y+direction[d][1]] == 0) {
}else {
d = (d+1)%4;
answer ++;
}
x+=direction[d][0];
y+=direction[d][1];
}
System.out.println(answer-1);
}
}