Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int max(int a,int b){
- return (a > b ? a : b);
- }
- int max4(int a,int b,int c,int d){
- return max(max(a,b),max(c,d));
- }
- int main(){
- int T;
- scanf("%d",&T);
- for(int t = 0 ; t < T ; t ++){
- int n,m;
- scanf("%d %d",&n,&m);
- char board[n][m+1];
- for(int i=0;i<n;i++){
- scanf("%s",board[i]);
- }
- int ngurUp[n][m];
- for(int i=0;i<n;i++)for(int j=0;j<m;j++)ngurUp[i][j] = (board[i][j] == '1' ? i : -1);
- for(int j=0;j<m;j++){
- for(int i=1;i<n;i++){
- if(ngurUp[i-1][j] != -1 && ngurUp[i][j] != -1){
- ngurUp[i][j] = ngurUp[i-1][j];
- }
- }
- }
- int ngurDown[n][m];
- for(int i=0;i<n;i++)for(int j=0;j<m;j++)ngurDown[i][j] = (board[i][j] == '1' ? i : -1);
- for(int j=0;j<m;j++){
- for(int i=n-2;i>=0;i--){
- if(ngurDown[i+1][j] != -1 && ngurDown[i][j] != -1){
- ngurDown[i][j] = ngurDown[i+1][j];
- }
- }
- }
- int ans = -2e9;
- for(int i=0;i<n;i++){
- int temp = 0;
- for(int j=0;j<m;j++){
- temp++;
- temp*=board[i][j] - '0';
- if(temp == 0)continue;
- ans = max4(ans,temp,temp + (i - ngurUp[i][j]),temp + (ngurDown[i][j] - i));
- }
- }
- for(int i=0;i<n;i++){
- int temp = 0;
- for(int j=m-1;j>=0;j--){
- temp++;
- temp*=board[i][j] - '0';
- if(temp == 0)continue;
- ans = max4(ans,temp,temp + (i - ngurUp[i][j]),temp + (ngurDown[i][j] - i));
- }
- }
- printf("%d\n",ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment