시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
7 초 256 MB 1255 320 185 25.947%

문제

음수가 아닌 정수들의 격자가 주어진다. 당신은 이 격자에 다음 연산을 행할 수 있다.

  • 1. 격자에서 가로 또는 세로로 인접한 정수 2개를 고른다.
  • 2. 각 정수가 양수일 때 1 감소시킨다.

다음 그림은 총 4개의 연속한 연산을 2*2 격자에 가해서 모든 정수를 0으로 만든 과정을 보여준다.

위 예제에서는 모든 정수를 0으로 만들기 위해 4번의 연산을 행했다. 이보다 적은 횟수의 연산으로는 모든 정수를 0으로 만들 수 없다는 것을 쉽게 알 수 있다.

격자가 주어졌을 때 모든 정수를 0으로 만들기 위해 필요한 최소 연산의 횟수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스의 첫째 줄에는 n과 m (2 ≤ n, m ≤ 50)이 주어지며, n은 격자의 행 개수, m은 격자의 열 개수를 나타낸다. 그 다음 n개의 줄에 각각 격자의 해당 행에 있는 m개의 정수가 열 순서대로 주어진다. 각 정수는 0 이상 1,000 이하이다.

출력

각 테스트 케이스마다 필요한 연산의 최소 횟수를 한 줄에 출력한다.

예제 입력 1

2
2 2
1 3
1 2
2 4
2 3 2 3
1 2 1 1

예제 출력 1

4
8
W3sicHJvYmxlbV9pZCI6IjExNDk1IiwicHJvYmxlbV9sYW5nIjoiMCIsInRpdGxlIjoiXHVhY2E5XHVjNzkwIDAgXHViOWNjXHViNGU0XHVhZTMwIiwiZGVzY3JpcHRpb24iOiI8cD5cdWM3NGNcdWMyMThcdWFjMDAgXHVjNTQ0XHViMmNjIFx1YzgxNVx1YzIxOFx1YjRlNFx1Yzc1OCBcdWFjYTlcdWM3OTBcdWFjMDAgXHVjOGZjXHVjNWI0XHVjOWM0XHViMmU0LiBcdWIyZjlcdWMyZTBcdWM3NDAgXHVjNzc0IFx1YWNhOVx1Yzc5MFx1YzVkMCBcdWIyZTRcdWM3NGMgXHVjNWYwXHVjMGIw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cnMsIG4gYW5kIG0gKDIgJmxlOyBuLCBtICZsZTsgNTApLCB3aGVyZSBuIGlzIHRoZSByb3cgc2l6ZSBhbmQgbSBpcyB0aGUgY29sdW1uIHNpemUgb2YgYSBncmlkLiBJbiB0aGUgZm9sbG93aW5nIG4gbGluZXMsIHRoZSBlbGVtZW50cyBvZiB0aGUgZ3JpZCBhcmUgZ2l2ZW4gcm93IGJ5IHJvdywgdGhhdCBpcywgdGhlIGktdGggbGluZSBjb250YWlucyBtIGludGVnZXJzIHdoaWNoIGNvcnJlc3BvbmQgdG8gdGhlIGVsZW1lbnRzIG9mIHRoZSBpLXRoIHJvdyBvZiB0aGUgZ3JpZCBpbiBvcmRlciwgZm9yIDEgJmxlOyBpICZsZTsgbi4gRWFjaCBpbnRlZ2VyIGluIHRoZSBncmlkIGlzIGJldHdlZW4gMCBhbmQgMSwwMDAsIGluY2x1c2l2ZWx5LjxcL3A+XHJcbiIsIm91dHB1dCI6IjxwPllvdXIgcHJvZ3JhbSBpcyB0byB3cml0ZSB0byBzdGFuZGFyZCBvdXRwdXQuIFByaW50IGV4YWN0bHkgb25lIGxpbmUgZm9yIGVhY2ggdGVzdCBjYXNlLiBUaGUgbGluZSBzaG91bGQgY29udGFpbiBhbiBpbnRlZ2VyIHJlcHJlc2VudGluZyB0aGUgbWluaW11bSBudW1iZXIgb2YgZXhlY3V0aW9ucyBvZiB0aGUgb3BlcmF0aW9ucyByZXF1aXJlZCBpbiB0aGUgcHJvYmxlbS48XC9wPlxyXG4iLCJoaW50IjoiIiwib3JpZ2luYWwiOiIxIiwicHJvYmxlbV9sYW5nX2NvZGUiOiJcdWM2MDFcdWM1YjQifV0=

출처

ACM-ICPC > Regionals > Asia > Korea > Asia Regional - Daejeon 2015 C번

  • 문제를 번역한 사람: kks227