1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| const LL N = 416; LL t, a, b, s[N][N];
inline LL getsum(int x1, int y1, int x2, int y2) { return s[x2][y2] + s[x1 - 1][y1 - 1] - s[x1 - 1][y2] - s[x2][y1 - 1]; }
inline LL getres(int x1, int y1, int x2, int y2) { LL sum2 = getsum(x1 + 1, y1 + 1, x2 - 1, y2 - 1), sum1 = getsum(x1 + 1, y1, x2 - 1, y1) + getsum(x1, y1 + 1, x1, y2 - 1) + getsum(x1 + 1, y2, x2 - 1, y2) + getsum(x2, y1 + 1, x2, y2 - 1); return 2 * (y2 - 1 - y1 + x2 - 1 - x1) - sum1 + sum2; }
int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> t; f(sb, 1, t) { char str[408]; memset(s, 0, sizeof(s)); cin >> a >> b; LL ma=a-4,mb=b-3; f(k, 1, a) { cin >> str; f(j, 1, b) { s[k][j] = s[k - 1][j] + s[k][j - 1] - s[k - 1][j - 1] + str[j - 1] - '0'; if(str[j - 1] - '0'){ ma=k<ma?k:ma; mb=j<mb?j:ma; } } } if (s[a][b] == 0) { cout << "10"; continue; } LL res = 16; if(a<400&&b<400){ ma=1; mb=1; } f(i, ma, a) { f(j, mb, b) { for (int k = i + 4; k <= a; k++) { for (int v = j + 3; v <= b; v++) { if (getsum(i + 1, j + 1, k - 1, v - 1) >= res) { break; } LL sp = getres(i, j, k, v); res = min(res, sp); if (s[a][b] - s[k][v] <= 2) break; } } } } cout << res << '\n'; } return 0; }
|